clickbot_100.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import random
  2. import sys
  3. import dataset
  4. from selenium import webdriver
  5. import traceback
  6. import datetime
  7. import codecs
  8. import time
  9. import urllib
  10. import argparse
  11. import schedule
  12. def restart_browser():
  13. options = webdriver.ChromeOptions()
  14. driver=webdriver.Chrome(options=options)
  15. driver.set_window_size(1400,1000)
  16. return driver
  17. def process_one():
  18. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  19. lst=[]
  20. cursor=db.query('select term from selected_kw where client="清原"')
  21. for c in cursor:
  22. lst.append(c['term'])
  23. term=random.choice(lst)
  24. print(term)
  25. driver=restart_browser()
  26. escaped_search_term=urllib.parse.quote(term)
  27. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(escaped_search_term, 100,'zh-TW')
  28. print(googleurl)
  29. driver.get(googleurl)
  30. time.sleep(6)
  31. elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a")
  32. for elmt in elmts:
  33. try:
  34. href=elmt.get_attribute('href')
  35. if 'taroboba-yuan.com' in href:
  36. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  37. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  38. print(href)
  39. print(elmt.text)
  40. break
  41. except:
  42. print('href2 exception')
  43. traceback.print_exc()
  44. driver.quit()
  45. process_one()
  46. parser = argparse.ArgumentParser()
  47. parser.add_argument('--loop')
  48. args = parser.parse_args()
  49. if args.loop:
  50. schedule.every(5).minutes.do(process_one)
  51. while True:
  52. schedule.run_pending()
  53. time.sleep(1)