smallset.py 8.0 KB

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