selwire_click.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import time
  2. import traceback
  3. import dataset
  4. from selenium import webdriver
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.keys import Keys
  9. from celery import Celery
  10. #import redis
  11. from celery.schedules import crontab
  12. import celery
  13. from celery.signals import celeryd_init
  14. from celery import signals
  15. import random
  16. from click import Option
  17. from selenium import webdriver
  18. import time
  19. import sys
  20. import os
  21. import zipfile
  22. from seleniumwire import webdriver
  23. import requests
  24. #from selenium import webdriver
  25. PROXY_PORT = 8080 # port
  26. PROXY_USER = 'choozmo' # username
  27. PROXY_PASS = 'Chuz83102968' # password
  28. custname=None
  29. param=sys.argv[1]
  30. rnd=True
  31. if len(sys.argv)>=3:
  32. rnd=False
  33. r = requests.get('http://db.ptt.cx:3001/api/push/'+param+'?status=up&msg=OK&ping=')
  34. #U1cGPOJAQD
  35. #http://db.ptt.cx:3001/api/push/hzmMjOaHpl?status=up&msg=OK&ping=
  36. def selenium_jared_click():
  37. global custname
  38. global rnd
  39. r=random.randint(0,4)
  40. options = {}
  41. if r==0:
  42. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080'}
  43. if r==1:
  44. options['proxy']={'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080'}
  45. if r==2:
  46. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080'}
  47. if r==3:
  48. options['proxy']={'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080'}
  49. print(options)
  50. # options = {
  51. # 'proxy': {
  52. # 'http': 'http://choozmo:Chuz83102968@172.105.205.52:8080'
  53. # 'http': 'http://choozmo:Chuz83102968@139.144.120.184:8080'
  54. # 'http': 'http://choozmo:Chuz83102968@172.104.92.245:8080'
  55. # 'http': 'http://choozmo:Chuz83102968@172.104.67.159:8080'
  56. # }
  57. # }
  58. c_options = webdriver.ChromeOptions()
  59. c_options.add_argument("--no-sandbox")
  60. c_options.add_argument("--headless")
  61. c_options.add_argument("--incognito")
  62. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  63. mobile_emulation = {
  64. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  65. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
  66. try:
  67. if rnd:
  68. driver = webdriver.Chrome(seleniumwire_options=options,options=c_options)
  69. else:
  70. driver = webdriver.Chrome(options=c_options)
  71. except:
  72. traceback.print_exc()
  73. #kw='真理大學國際生'
  74. #domain='cia.au.edu.tw'
  75. if custname is None:
  76. custname='真理'
  77. cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='"+custname+"' order by random() limit 1")
  78. kw=None
  79. domain=None
  80. for c in cursor:
  81. cust=c['cust']
  82. kw=c['kw']
  83. plan=c['plan']
  84. prefix=c['prefix']
  85. domain=eval(c['domain'])[0]
  86. positive=eval(c['positive'])
  87. break
  88. #kw='真理大學教堂'
  89. #domain='udn.com'
  90. print(kw)
  91. print(domain)
  92. #kw='真理大學校友'
  93. #kw='真理大學國際生'
  94. #kw='真理大學張聰聯'
  95. #domain='au.edu.tw'
  96. #domain='pronews.tw'
  97. driver.get('https://www.google.com?num=100')
  98. time.sleep(3)
  99. print(driver.current_url)
  100. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  101. time.sleep(1)
  102. elmt.send_keys(kw)
  103. elmt.send_keys(Keys.ENTER)
  104. time.sleep(4)
  105. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  106. numresults=len(elmts)
  107. print('搜尋結果數量',numresults)
  108. if numresults<=0:
  109. print(elmts)
  110. for elmt in elmts:
  111. href=elmt.get_attribute('href')
  112. txt=elmt.text
  113. # print(txt)
  114. # print(href)
  115. if domain in href:
  116. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  117. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  118. print(domain)
  119. print(href)
  120. driver.quit()
  121. return txt
  122. break
  123. return '{empty}'
  124. # time.sleep(5)
  125. selenium_jared_click()