|
@@ -0,0 +1,124 @@
|
|
|
+from selenium import webdriver
|
|
|
+from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
|
+import time
|
|
|
+import os
|
|
|
+import urllib.parse
|
|
|
+from selenium.webdriver.support.ui import WebDriverWait
|
|
|
+from selenium.webdriver.common.by import By
|
|
|
+from selenium.webdriver.support import expected_conditions as EC
|
|
|
+import codecs
|
|
|
+import random
|
|
|
+from bs4 import BeautifulSoup
|
|
|
+import requests
|
|
|
+import time
|
|
|
+import redis
|
|
|
+import time
|
|
|
+
|
|
|
+
|
|
|
+headers = {
|
|
|
+ "Authorization": "Bearer " + "t35vhZtWNgvDNWHc3DJh0OKll3mcB9GvC8K2EAkBug2",
|
|
|
+ "Content-Type": "application/x-www-form-urlencoded"
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+def send_msg(kw):
|
|
|
+ params = {"message": "處理關鍵字: "+kw}
|
|
|
+ r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
|
|
|
+
|
|
|
+
|
|
|
+def empty_query(q):
|
|
|
+ global driver
|
|
|
+ googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
|
|
|
+ driver.get(googleurl)
|
|
|
+ time.sleep(3)
|
|
|
+
|
|
|
+
|
|
|
+def process_query(q):
|
|
|
+ global driver
|
|
|
+ googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
|
|
|
+ driver.get(googleurl)
|
|
|
+ time.sleep(3)
|
|
|
+
|
|
|
+ elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
|
|
|
+ idx=1
|
|
|
+ ranking=-1
|
|
|
+ for elmt in elmts:
|
|
|
+ href=elmt.get_attribute('href')
|
|
|
+ txt=elmt.text
|
|
|
+ if len(txt)>10:
|
|
|
+
|
|
|
+ if 'hhh.com.tw' in href:
|
|
|
+
|
|
|
+ webdriver.ActionChains(driver).move_to_element(elmt).perform()
|
|
|
+ webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
|
|
|
+ break
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+driver = webdriver.Remote(
|
|
|
+
|
|
|
+
|
|
|
+command_executor='http://dev2.choozmo.com:14444/wd/hub',
|
|
|
+desired_capabilities=DesiredCapabilities.CHROME)
|
|
|
+driver.set_window_size(1400,1000)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+r = redis.Redis(host='db.ptt.cx', port=6379, db=1)
|
|
|
+p = r.pubsub(ignore_subscribe_messages=True)
|
|
|
+p.subscribe('q_hhh_seo')
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+while True:
|
|
|
+ message = p.get_message()
|
|
|
+ if message:
|
|
|
+ q=message['data'].decode('utf-8')
|
|
|
+ print(q)
|
|
|
+ prob=random.randint(0,3)
|
|
|
+ if prob ==0:
|
|
|
+ print(q)
|
|
|
+ process_query(q)
|
|
|
+ send_msg(q)
|
|
|
+ else:
|
|
|
+ empty_query(q)
|
|
|
+ intsleep=random.randint(5,10)
|
|
|
+
|
|
|
+ time.sleep(intsleep)
|
|
|
+
|
|
|
+
|