pool_bad_clicks.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 os
  23. import time
  24. import requests
  25. driver=None
  26. dockername='p4444'
  27. #is_docker=True
  28. is_docker=False
  29. db = dataset.connect('postgresql://postgres:eyJhbGciOiJI@172.105.241.163:5432/postgres')
  30. if is_docker:
  31. portnum=random.randint(4444,4555)
  32. print(portnum)
  33. os.system('docker container stop '+dockername)
  34. time.sleep(0.5)
  35. os.system('docker container rm '+dockername)
  36. time.sleep(0.5)
  37. os.system('docker run -d -p '+str(portnum)+':4444 --shm-size=2g --name '+dockername+' --dns 168.95.1.1 selenium/standalone-chrome:103.0')
  38. time.sleep(7)
  39. def re_get_webdriver():
  40. global port
  41. global driver
  42. global portnum
  43. global is_docker
  44. result=[]
  45. if driver is not None:
  46. print('closing....')
  47. driver.quit()
  48. print('quit....')
  49. driver=None
  50. try:
  51. options = webdriver.ChromeOptions()
  52. options.add_argument("--no-sandbox")
  53. options.add_argument("--headless")
  54. options.add_argument("--incognito")
  55. # options.add_argument('--proxy-server=socks5://172.104.92.245:14900')
  56. mobile_emulation = {
  57. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  58. "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" }
  59. # options.add_experimental_option("mobileEmulation", mobile_emulation)
  60. if is_docker:
  61. try:
  62. driver = webdriver.Remote(
  63. command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub',
  64. options=options)
  65. except:
  66. traceback.print_exc()
  67. time.sleep(9999)
  68. return None
  69. return driver
  70. try:
  71. driver = webdriver.Chrome(options=options)
  72. except:
  73. traceback.print_exc()
  74. return None
  75. return driver
  76. except:
  77. traceback.print_exc()
  78. driver=None
  79. return None
  80. return driver
  81. def run_once(jsobj):
  82. global db
  83. table=db['bad_clicks']
  84. print(jsobj)
  85. kw=jsobj['kw']
  86. i=100
  87. while True:
  88. driver=re_get_webdriver()
  89. print('re_get_webdriver')
  90. if driver is not None:
  91. break
  92. time.sleep(3)
  93. try:
  94. kw=jsobj['kw']
  95. if jsobj.get('domain') is None:
  96. exclude=jsobj['exclude']
  97. domain=None
  98. else:
  99. domain=jsobj['domain']
  100. exclude=None
  101. driver.get('https://www.google.com?num=100')
  102. time.sleep(1)
  103. while True:
  104. try:
  105. print(driver.current_url)
  106. break
  107. except:
  108. traceback.print_exc()
  109. driver=re_get_webdriver()
  110. time.sleep(3)
  111. driver.get('https://www.google.com?num=100')
  112. # time.sleep(3)
  113. time.sleep(3)
  114. elmt = driver.find_element(By.XPATH, "//textarea[@type='search']")
  115. time.sleep(1)
  116. elmt.send_keys(kw)
  117. elmt.send_keys(Keys.ENTER)
  118. time.sleep(3)
  119. # elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  120. elmts = driver.find_elements(By.XPATH, "//a[@jsname='UWckNb']")
  121. numresults=len(elmts)
  122. print('搜尋結果數量',numresults)
  123. if numresults==0:
  124. print(driver.current_url)
  125. print(driver.title)
  126. sys.exit()
  127. # time.sleep(9999)
  128. idx=1
  129. found=False
  130. test_lst=[]
  131. clickelmt=None
  132. neg_count=0
  133. neg_total=0
  134. clickidx=0
  135. clickhref=''
  136. clicktitle=''
  137. for elmt in elmts:
  138. href=elmt.get_attribute('href')
  139. txt=elmt.text
  140. if '坑殺' in txt or '侵占' in txt or '判決書' in txt or '強佔' in txt or '掏空' in txt or '送達公告' in txt or '違反勞動'in txt:
  141. neg_count+=1
  142. neg_total+=idx
  143. if len(txt)>10:
  144. if domain is not None:
  145. random.shuffle(domain)
  146. for d in domain:
  147. if d in href:
  148. print('found....')
  149. print('clicked....')
  150. print(href)
  151. print(txt)
  152. print("ranking", idx)
  153. found=True
  154. clickelmt=elmt
  155. clickidx=idx
  156. clickhref=href
  157. clicktitle=txt
  158. else:
  159. if exclude not in href:
  160. test_lst.append(elmt)
  161. idx+=1
  162. if exclude is not None:
  163. print('exclude')
  164. elmt=random.choice(test_lst)
  165. print(elmt)
  166. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  167. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  168. time.sleep(3)
  169. if neg_count ==0:
  170. negstr='0'
  171. else:
  172. negstr=str(neg_total/neg_count)
  173. print(' negative: ' +negstr)
  174. if not found:
  175. True
  176. table.insert({'ranking':-1,'kw':kw,'results':numresults,'url':'','title':'未收錄'})
  177. else:
  178. webdriver.ActionChains(driver).move_to_element(clickelmt).perform()
  179. webdriver.ActionChains(driver).move_to_element(clickelmt).click().perform()
  180. print('clicked...')
  181. table.insert({'ranking':clickidx,'kw':kw,'results':numresults,'url':clickhref,'title':clicktitle,'avg_neg':negstr})
  182. time.sleep(6)
  183. print('sleep 6')
  184. return
  185. except:
  186. traceback.print_exc()
  187. print('exception')
  188. traceback.print_exc()
  189. driver.quit()
  190. time.sleep(5)
  191. r=random.randint(0,24)
  192. print(r)
  193. #r=987
  194. #r=
  195. #JNOTE: 關鍵字點擊
  196. related=''
  197. r=5
  198. #r=6
  199. #聲明
  200. entries=[]
  201. entry={'cust':'啟翔',
  202. 'plan':'文章',
  203. 'prefix':'',
  204. 'kw':'1111 4970-808',
  205. 'postfix':'',
  206. 'domain':['Comp_Info.aspx?vNo=48173'],
  207. 'positive':['']}
  208. entries.append(entry)
  209. entry={'cust':'啟翔',
  210. 'plan':'文章',
  211. 'prefix':'',
  212. 'kw':'作業員 4970808',
  213. 'postfix':'',
  214. 'domain':['job/3uhrh'],
  215. 'positive':['']}
  216. entries.append(entry)
  217. entry={'cust':'啟翔',
  218. 'plan':'文章',
  219. 'prefix':'',
  220. 'kw':'facebook 100078739363391 啟翔',
  221. 'postfix':'',
  222. 'domain':['100078739363391'],
  223. 'positive':['']}
  224. entries.append(entry)
  225. entry={'cust':'啟翔',
  226. 'plan':'文章',
  227. 'prefix':'',
  228. 'kw':'廢水專責人員 104 啟翔',
  229. 'postfix':'',
  230. 'domain':['job/7wih0'],
  231. 'positive':['']}
  232. entries.append(entry)
  233. entry={'cust':'加百裕',
  234. 'plan':'文章',
  235. 'prefix':'',
  236. 'kw':'黃靖容中醫',
  237. 'postfix':'',
  238. 'domain':['web.csh.org.tw'],
  239. 'positive':['']}
  240. entries.append(entry)
  241. entry=random.choice(entries)
  242. #朱英凱
  243. #琢隱設計
  244. #os.system('curl --socks5 choozmo:choozmo9@172.104.92.245:14900 http://www.google.com')
  245. newkw=entry['prefix']+" "+entry['kw']+' '+random.choice(entry['positive'])
  246. print(newkw)
  247. #newkw=kw
  248. run_once({'domain':entry['domain'],'kw':newkw})