clickbot_100.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import dataset
  2. from selenium import webdriver
  3. import traceback
  4. import time
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.common.by import By
  7. import pymysql
  8. pymysql.install_as_MySQLdb()
  9. path = 'C:\portable\chromedriver'
  10. path_z = '/Users/zooeytsai/Downloads/chromedriver 4'
  11. def restart_browser():
  12. options = webdriver.ChromeOptions()
  13. # options.add_argument("user-agent=%s" % rua())
  14. options.add_argument('--headless')
  15. driver=webdriver.Chrome(options=options,executable_path=path_z)
  16. driver.set_window_size(950, 20000)
  17. return driver
  18. def process_one(kw,domain):
  19. # db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  20. db_local = dataset.connect('mysql://root:jondae350@localhost/ig_tags')
  21. table=db_local['google_rank']
  22. driver=restart_browser()
  23. # escaped_search_term=urllib.parse.quote(term)
  24. googleurl = 'https://www.google.com/?num=100'
  25. driver.get(googleurl)
  26. time.sleep(6)
  27. send_kw_elmt = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
  28. send_kw_elmt.send_keys(kw)
  29. time.sleep(3)
  30. send_kw_elmt.send_keys(Keys.ENTER)
  31. time.sleep(6)
  32. elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  33. cnt=1
  34. datadict={'搜尋詞':[],'結果標題':[],'結果網址':[],'結果名次':[]}
  35. for elmt in elmts:
  36. try:
  37. href=elmt.get_attribute('href')
  38. if domain in href:
  39. datadict['搜尋詞'].append(kw)
  40. datadict['結果標題'].append(elmt.text)
  41. datadict['結果網址'].append(href)
  42. datadict['結果名次'].append(str(cnt))
  43. cnt+=1
  44. except:
  45. print('href2 exception')
  46. traceback.print_exc()
  47. driver.quit()
  48. time.sleep(60)
  49. # db.close()
  50. db_local.close()
  51. return datadict