gen_seo2.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import traceback
  2. import dataset
  3. from selenium import webdriver
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.common.keys import Keys
  6. from selenium.webdriver.chrome.service import Service
  7. import json
  8. import redis
  9. import sys
  10. import random
  11. import os
  12. import time
  13. from userAgentRandomizer import userAgents
  14. import requests
  15. #import pymysql
  16. #pymysql.install_as_MySQLdb()
  17. driver = None
  18. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  19. headers = {
  20. "Authorization": "Bearer " + "6SDULL1Ebklduc6TFxa97AFto5Sj21kyJ30CxiLiSoi",
  21. "Content-Type": "application/x-www-form-urlencoded"
  22. }
  23. def send_msg(kw):
  24. params = {"message": kw}
  25. r = requests.post("https://notify-api.line.me/api/notify", headers=headers, params=params)
  26. def re_get_webdriver():
  27. global port
  28. global driver
  29. result = []
  30. if driver is not None:
  31. print('closing....')
  32. driver.quit()
  33. os.system('killall chrome')
  34. print('quit....')
  35. driver = None
  36. try:
  37. s = Service('/root/driver/chromedriver102')
  38. options = webdriver.ChromeOptions()
  39. options.add_argument("--disable-dev-shm-usage")
  40. options.add_argument("--headless")
  41. options.add_argument('--remote-debugging-port=9222')
  42. options.add_experimental_option("debuggerAddress", '127.0.0.1:9927')
  43. options.add_argument("--incognito")
  44. r = redis.Redis(host='db.ptt.cx', port=6379, db=2, password='choozmo9')
  45. data = r.get('google_proxy')
  46. jstext = data.decode('utf-8')
  47. jsobj = json.loads(jstext)
  48. proxy = random.choice(jsobj)
  49. change_ip_list = ['--proxy-server=%s' % proxy, "--proxy-server=socks5://127.0.0.1:9050",
  50. "--proxy-server=socks5://192.53.174.202:8180"]
  51. change_ip = random.choice(change_ip_list)
  52. options.add_argument(change_ip)
  53. print('使用代理ip', change_ip)
  54. driver = webdriver.Chrome(options=options,service=s)
  55. #driver.delete_all_cookies()
  56. driver.set_window_size(1400, 1000)
  57. except:
  58. traceback.print_exc()
  59. driver = None
  60. return None
  61. def run_once(jsobj):
  62. table = db['rank_detection']
  63. print(jsobj)
  64. global driver
  65. # i=random.randint(0,9)
  66. i = 100
  67. if driver is None:
  68. time.sleep(8)
  69. re_get_webdriver()
  70. if driver is None:
  71. return
  72. try:
  73. kw = jsobj['kw']
  74. if jsobj.get('domain') is None:
  75. exclude = jsobj['exclude']
  76. domain = None
  77. else:
  78. domain = jsobj['domain']
  79. exclude = None
  80. # driver.get('https://www.google.com?num=100')
  81. driver.get('https://www.google.com?num=20')
  82. time.sleep(3)
  83. print(driver.current_url)
  84. elmt = driver.find_element(By.XPATH, "//input[@name='q']")
  85. time.sleep(1)
  86. elmt.send_keys(kw)
  87. elmt.send_keys(Keys.ENTER)
  88. time.sleep(6)
  89. print(driver.current_url)
  90. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  91. numresults = len(elmts)
  92. print('搜尋結果數量', numresults)
  93. if numresults == 0:
  94. send_msg('stop working...')
  95. sys.exit()
  96. idx = 1
  97. found = False
  98. test_lst = []
  99. txt_dict = {}
  100. for elmt in elmts:
  101. href = elmt.get_attribute('href')
  102. txt = elmt.text
  103. if len(txt) > 10:
  104. if domain is not None:
  105. if domain in href:
  106. print('found....')
  107. print('clicked....')
  108. print(href)
  109. print(txt)
  110. print("ranking", idx)
  111. found = True
  112. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  113. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  114. table.insert({'ranking': idx, 'kw': kw, 'results': numresults, 'url': href, 'title': txt})
  115. time.sleep(6)
  116. break
  117. else:
  118. ex = False
  119. for ee in exclude:
  120. if ee in href:
  121. ex = True
  122. if not ex:
  123. test_lst.append(elmt)
  124. txt_dict[elmt] = txt
  125. idx += 1
  126. if exclude is not None:
  127. print('exclude')
  128. elmt = random.choice(test_lst[5:])
  129. print(elmt)
  130. print(txt_dict[elmt])
  131. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  132. elmt.click()
  133. # webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  134. time.sleep(5)
  135. if not found:
  136. table.insert({'ranking': -1, 'kw': kw, 'results': numresults, 'url': '', 'title': '未收錄'})
  137. except:
  138. print('exception')
  139. traceback.print_exc()
  140. sys.exit()
  141. driver.quit()
  142. db.close()
  143. # par1=sys.argv[1]
  144. # port=sys.argv[2]
  145. # kws=['職籃','PLG','高雄','鋼鐵人','內幕','中資','股東','姊夫','中國','老賴','香港','無極','原始股東','外資','董事長','股權結構','高雄人','黑人','陳建州','職籃聯盟','球團','球團高層','香港無極','張憲銘','吳同喬','監察人']
  146. kws = ['金融', '人才', '國際接軌', '國際', '投資金童', '投資', '金童', '對沖基金', '香港', '外資', '原始股東', '職籃', 'PLG', '職籃聯盟', '球團', '台灣女婿',
  147. '抹紅', '保守', '港元', '美國', '升息', '戰爭', '通膨', '亞洲', '亞洲投資金童']
  148. positive = ['錢濤','亞洲投資金童', '錢濤 職籃夢']
  149. os.system('docker container restart tiny6')
  150. kw = random.choice(kws)
  151. # time.sleep(9)
  152. # run_once({'domain':'ettoday.net','kw':'錢濤'})
  153. # run_once({'exclude':['moreptt.com','ptt.cc','tnews.cc','mirrormedia.mg','newtalk.tw','pourquoi.tw','match.net.tw','freshweekly.tw','z-upload.facebook.com','udn.com'],'kw':kw+' 錢濤'})
  154. domains = ['yahoo.com', 'ettoday.net', 'tvbs.com.tw', 'sina.com.tw', 'ltn.com.tw', 'owlting.com', 'ctee.com.tw']
  155. domain = random.choice(domains)
  156. p = random.choice(positive)
  157. # run_once({'domain':domain,'kw':p})
  158. run_once({'domain': 'ettoday.net', 'kw': p})
  159. # run_once({'domain':domain,'kw':kw+' 錢濤'})