click_twice.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #import redis
  2. import time
  3. import traceback
  4. #import json
  5. from selenium import webdriver
  6. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  7. import time
  8. #import urllib
  9. import os
  10. from selenium.webdriver.support.ui import WebDriverWait
  11. from selenium.webdriver.common.by import By
  12. from selenium.webdriver.support import expected_conditions as EC
  13. import dataset
  14. from selenium.webdriver.common.keys import Keys
  15. import json
  16. import random
  17. import time
  18. #import redis
  19. import sys
  20. import codecs
  21. import random
  22. import platform
  23. import socket
  24. import os
  25. import time
  26. import datetime
  27. import requests
  28. #JNOTE: CLICK TWICE
  29. driver=None
  30. dockername='p4444'
  31. is_docker=True
  32. #is_docker=False
  33. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  34. table=db['prog_launch']
  35. unamestr=str(platform.uname())
  36. table.insert({'uname':unamestr,'progname':os.path.basename(__file__),'dt':datetime.datetime.now()})
  37. url_white=[]
  38. cursor=db.query("select url from seo_whitelist where cust='啟翔'")
  39. for c in cursor:
  40. url_white.append(c['url'])
  41. print(url_white)
  42. if len(url_white) <=0:
  43. print('url_white loading failed')
  44. sys.exit()
  45. #sys.exit()
  46. #db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  47. def scrolling(driver,pgnum):
  48. # ub = driver.find_element_by_css_selector('body')
  49. ub = driver.find_element(By.CSS_SELECTOR,'body')
  50. for i in range(pgnum):
  51. ub.send_keys(Keys.PAGE_DOWN)
  52. if pgnum>1:
  53. time.sleep(0.3)
  54. if is_docker:
  55. portnum=random.randint(4444,4555)
  56. print(portnum)
  57. os.system('docker container stop '+dockername)
  58. time.sleep(0.5)
  59. os.system('docker container rm '+dockername)
  60. time.sleep(0.5)
  61. os.system('docker run -d -p '+str(portnum)+':4444 --shm-size=2g --name '+dockername+' --dns 168.95.1.1 selenium/standalone-chrome:103.0')
  62. time.sleep(7)
  63. def re_get_webdriver():
  64. global port
  65. global driver
  66. global portnum
  67. global is_docker
  68. result=[]
  69. if driver is not None:
  70. print('closing....')
  71. driver.quit()
  72. print('quit....')
  73. driver=None
  74. try:
  75. options = webdriver.ChromeOptions()
  76. options.add_argument("--no-sandbox")
  77. options.add_argument("--headless")
  78. options.add_argument("--incognito")
  79. # options.add_argument('--proxy-server=socks5://172.104.92.245:14900')
  80. mobile_emulation = {
  81. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  82. "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" }
  83. # options.add_experimental_option("mobileEmulation", mobile_emulation)
  84. if is_docker:
  85. try:
  86. driver = webdriver.Remote(
  87. command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub',
  88. options=options)
  89. except:
  90. traceback.print_exc()
  91. time.sleep(9999)
  92. return None
  93. return driver
  94. try:
  95. driver = webdriver.Chrome(options=options)
  96. except:
  97. traceback.print_exc()
  98. return None
  99. return driver
  100. except:
  101. traceback.print_exc()
  102. driver=None
  103. return None
  104. return driver
  105. def click_allowed():
  106. global driver
  107. global url_white
  108. elmts = driver.find_elements(By.XPATH, "//a")
  109. elmt_lst=[]
  110. for elmt in elmts:
  111. elmt_lst.append(elmt)
  112. random.shuffle(elmt_lst)
  113. for elmt in elmt_lst:
  114. href=elmt.get_attribute('href')
  115. for url in url_white:
  116. if url in href:
  117. print(href)
  118. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  119. time.sleep(1)
  120. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  121. time.sleep(5)
  122. return
  123. def run_once(jsobj):
  124. table=db['seo_jobs_ranking']
  125. # history=db['seo_search_history']
  126. print(jsobj)
  127. kw=jsobj['kw']
  128. i=100
  129. while True:
  130. driver=re_get_webdriver()
  131. print('re_get_webdriver')
  132. if driver is not None:
  133. break
  134. time.sleep(3)
  135. try:
  136. kw=jsobj['kw']
  137. if jsobj.get('domain') is None:
  138. exclude=jsobj['exclude']
  139. domain=None
  140. else:
  141. domain=jsobj['domain']
  142. exclude=None
  143. driver.get('https://www.google.com?num=100')
  144. time.sleep(1)
  145. while True:
  146. try:
  147. print(driver.current_url)
  148. break
  149. except:
  150. traceback.print_exc()
  151. driver=re_get_webdriver()
  152. time.sleep(3)
  153. driver.get('https://www.google.com?num=100')
  154. # time.sleep(3)
  155. time.sleep(3)
  156. # time.sleep(9999)
  157. # elmt = driver.find_element(By.XPATH, "//input[@name='q']")
  158. elmt = driver.find_element(By.XPATH, "//textarea[@name='q']")
  159. # elmt = driver.find_element(By.XPATH, "//textarea[@type='search']")
  160. time.sleep(1)
  161. elmt.send_keys(kw)
  162. elmt.send_keys(Keys.ENTER)
  163. time.sleep(3)
  164. # elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  165. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  166. numresults=len(elmts)
  167. print('搜尋結果數量',numresults)
  168. if numresults==0:
  169. print(driver.current_url)
  170. print(driver.title)
  171. sys.exit()
  172. # time.sleep(9999)
  173. idx=1
  174. found=False
  175. test_lst=[]
  176. clickelmt=None
  177. neg_count=0
  178. neg_total=0
  179. clickidx=0
  180. clickhref=''
  181. clicktitle=''
  182. for elmt in elmts:
  183. href=elmt.get_attribute('href')
  184. txt=elmt.text
  185. # history.insert({'ranking':idx,'kw':kw,'results':numresults,'url':href,'title':txt})
  186. if '坑殺' in txt or '侵占' in txt or '判決書' in txt or '強佔' in txt or '掏空' in txt or '送達公告' in txt or '違反勞動'in txt:
  187. neg_count+=1
  188. neg_total+=idx
  189. if len(txt)>10:
  190. if domain is not None:
  191. random.shuffle(domain)
  192. for d in domain:
  193. if d in href:
  194. print('found....')
  195. print('clicked....')
  196. print(href)
  197. print(txt)
  198. print("ranking", idx)
  199. found=True
  200. clickelmt=elmt
  201. clickidx=idx
  202. clickhref=href
  203. clicktitle=txt
  204. else:
  205. if exclude not in href:
  206. test_lst.append(elmt)
  207. idx+=1
  208. if exclude is not None:
  209. print('exclude')
  210. elmt=random.choice(test_lst)
  211. print(elmt)
  212. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  213. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  214. scrolling(driver,3)
  215. time.sleep(5)
  216. if neg_count ==0:
  217. negstr='0'
  218. else:
  219. negstr=str(neg_total/neg_count)
  220. print(' negative: ' +negstr)
  221. if not found:
  222. True
  223. table.insert({'ranking':-1,'kw':kw,'results':numresults,'url':'','title':'未收錄'})
  224. else:
  225. webdriver.ActionChains(driver).move_to_element(clickelmt).perform()
  226. webdriver.ActionChains(driver).move_to_element(clickelmt).click().perform()
  227. print('clicked...')
  228. table.insert({'ranking':clickidx,'kw':kw,'results':numresults,'url':clickhref,'title':clicktitle,'avg_neg':negstr})
  229. scrolling(driver,3)
  230. time.sleep(6)
  231. print('sleep 6')
  232. click_allowed()
  233. return
  234. except:
  235. traceback.print_exc()
  236. print('exception')
  237. traceback.print_exc()
  238. driver.quit()
  239. time.sleep(5)
  240. #r=random.randint(0,7)
  241. #r=987
  242. #JNOTE: 關鍵字點擊
  243. related=''
  244. #cursor=db.query('SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs order by random() limit 1')
  245. #cursor=db.query("SELECT cust,plan,prefix,domain,kw,positive FROM public.seo_jobs where cust='啟翔' order by random() limit 1")
  246. cursor=db.query("SELECT cust,kw,url FROM public.seo_doublejob where cust='啟翔' order by random() limit 1")
  247. for c in cursor:
  248. cust=c['cust']
  249. kw=c['kw']
  250. plan=''
  251. prefix=''
  252. domain=[c['url']]
  253. positive=['']
  254. # positive=eval(c['positive'])
  255. break
  256. r=9999
  257. if r==11:
  258. cust='啟翔'
  259. plan='形象SEO'
  260. postfix=''
  261. domain=['abba-tech-aluminum']
  262. kw='啟翔輕金屬產品'
  263. prefix=''
  264. positive=['']
  265. # positive=['集仕多']
  266. # positive=['集仕多 AIGV']
  267. # positive=['集仕多 三立']
  268. # positive=['台北室內設計公司排名']
  269. # positive=[related]
  270. # positive=['半 日照 植物 推薦']
  271. # positive=['3 坪 多大']
  272. # positive=['鞋櫃']
  273. # positive=['裝修屋子']
  274. # positive=['']
  275. # kw='幸福空間'
  276. # kw='輕裝修'
  277. # kw='輕裝修'
  278. #朱英凱
  279. #琢隱設計
  280. #os.system('curl --socks5 choozmo:choozmo9@172.104.92.245:14900 http://www.google.com')
  281. #newkw=prefix+" "+kw+' '+random.choice(positive)
  282. newkw=kw
  283. print(newkw)
  284. #newkw=kw
  285. run_once({'domain':domain,'kw':newkw})