general_gsrack.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import time
  2. import json
  3. from selenium import webdriver
  4. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  5. import time
  6. import os
  7. import urllib.parse
  8. from selenium.webdriver.support.ui import WebDriverWait
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.chrome.service import Service
  11. from selenium.webdriver.support import expected_conditions as EC
  12. import codecs
  13. import random
  14. import requests
  15. import datetime
  16. import dataset
  17. import time
  18. import traceback
  19. import sys
  20. import fire
  21. import random
  22. import pymysql
  23. pymysql.install_as_MySQLdb()
  24. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  25. table = db['general_log']
  26. driver = None
  27. def rua():
  28. pool = [
  29. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
  30. "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0",
  31. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
  32. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36",
  33. "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
  34. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
  35. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125",
  36. ]
  37. return random.choice(pool)
  38. def empty_query(q):
  39. global driver
  40. googleurl = 'https://www.google.com/search?q=' + urllib.parse.quote(q)
  41. driver.get(googleurl)
  42. time.sleep(3)
  43. def process_query(qs):
  44. q = qs[0]
  45. domain = qs[1]
  46. global driver
  47. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100, 'zh-TW')
  48. print(googleurl)
  49. driver.get(googleurl)
  50. time.sleep(6)
  51. while True:
  52. try:
  53. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  54. print('尋找')
  55. break
  56. except:
  57. pass
  58. idx = 1
  59. ranking = -1
  60. print('搜尋結果數量', len(elmts))
  61. # driver.save_screenshot('c:/tmp/test.png')
  62. for elmt in elmts:
  63. href = elmt.get_attribute('href')
  64. txt = elmt.text
  65. if len(txt) > 10:
  66. if domain in href:
  67. print('clicked....')
  68. print(href)
  69. print(txt)
  70. print("ranking", idx)
  71. table.insert({'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href,
  72. 'dt': datetime.datetime.now(), 'num': 1})
  73. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  74. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  75. break
  76. idx += 1
  77. def run_once(q):
  78. global driver
  79. result = []
  80. s = Service('/root/driver/chromedriver')
  81. # s = Service('/Users/zooeytsai/Downloads/chromedriver 2')
  82. user_agent = rua()
  83. options = webdriver.ChromeOptions()
  84. options.add_argument('--headless')
  85. options.add_argument('--remote-debugging-port=9222')
  86. options.add_experimental_option("debuggerAddress", "127.0.0.1:{q[2]}")
  87. options.add_argument("--user-agent=" + user_agent)
  88. options.add_argument("--incognito")
  89. driver = webdriver.Chrome(options=options, service=s)
  90. driver.delete_all_cookies()
  91. driver.set_window_size(1400, 1000)
  92. print('到此')
  93. process_query(q)
  94. time.sleep(3)
  95. driver.quit()
  96. # for c in lst:
  97. # while True:
  98. # try:
  99. # c=random.choice(lst)
  100. # except:
  101. # traceback.print_exc()
  102. # sleepint=random.randint(320,520)
  103. # time.sleep(sleepint)
  104. class JParams(object):
  105. def get(self, kw, domain, port):
  106. print(kw)
  107. print(domain)
  108. run_once((kw, domain, port))
  109. if __name__ == '__main__':
  110. fire.Fire(JParams)
  111. def get(self, kw, domain, port):
  112. print('kw')
  113. print(domain)
  114. run_once((kw, domain, port))