google_rank.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 pymysql
  22. pymysql.install_as_MySQLdb()
  23. driver = None
  24. def empty_query(q):
  25. global driver
  26. googleurl = 'https://www.google.com/search?q=' + urllib.parse.quote(q)
  27. driver.get(googleurl)
  28. time.sleep(3)
  29. def process_query(qs):
  30. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  31. table = db['google_rank']
  32. q = qs
  33. global driver
  34. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100, 'zh-TW')
  35. driver.get(googleurl)
  36. print(driver.current_url)
  37. time.sleep(6)
  38. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  39. idx = 1
  40. ranking = -1
  41. print(len(elmts))
  42. # driver.save_screenshot('C:\/Users\/s1301\/Pictures\/Saved Pictures\/')
  43. for elmt in elmts:
  44. href = elmt.get_attribute('href')
  45. txt = elmt.text
  46. table.insert({'title': elmt.text, 'url': href, 'keyword': q, 'dt': datetime.datetime.now(), 'ranking': idx})
  47. idx += 1
  48. db.close()
  49. def run_once(q):
  50. global driver
  51. result = []
  52. user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
  53. s = Service('C:\/Users\/s1301\/Downloads\/chromedriver_107\/chromedriver')
  54. options = webdriver.ChromeOptions()
  55. options.add_argument('--headless')
  56. # options.add_argument('--remote-debugging-port=9222')
  57. # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
  58. options.add_argument("--user-agent=" +user_agent)
  59. options.add_argument("--incognito")
  60. driver = webdriver.Chrome(
  61. options=options, service=s)
  62. str1 = driver.capabilities['browserVersion']
  63. print('版本', str1)
  64. driver.delete_all_cookies()
  65. driver.set_window_size(1400, 1000)
  66. print(q)
  67. process_query(q)
  68. time.sleep(3)
  69. driver.quit()
  70. # for c in lst:ㄕ
  71. # while True:
  72. # try:
  73. # c=random.choice(lst)
  74. # except:
  75. # traceback.print_exc()
  76. # sleepint=random.randint(320,520)
  77. # time.sleep(sleepint)
  78. lst = ['信義房仲','信義 房屋','信義 房仲','雙響泡']
  79. for i in lst:
  80. run_once(i)