|
@@ -0,0 +1,99 @@
|
|
|
+import time
|
|
|
+import json
|
|
|
+from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
|
|
|
+import os
|
|
|
+import urllib.parse
|
|
|
+from selenium.webdriver.support.ui import WebDriverWait
|
|
|
+from selenium import webdriver
|
|
|
+from selenium.webdriver.common.by import By
|
|
|
+from selenium.webdriver.chrome.service import Service
|
|
|
+from selenium.webdriver.common.keys import Keys
|
|
|
+from selenium.webdriver.support import expected_conditions as EC
|
|
|
+import codecs
|
|
|
+import random
|
|
|
+import requests
|
|
|
+import datetime
|
|
|
+import dataset
|
|
|
+import time
|
|
|
+import traceback
|
|
|
+import sys
|
|
|
+import fire
|
|
|
+import pymysql
|
|
|
+pymysql.install_as_MySQLdb()
|
|
|
+
|
|
|
+driver = None
|
|
|
+
|
|
|
+
|
|
|
+def process_query(qs):
|
|
|
+ db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
|
|
|
+ table = db['ads']
|
|
|
+ q = qs[0]
|
|
|
+ client = qs[1]
|
|
|
+ global driver
|
|
|
+ googleurl = 'https://www.google.com/webhp?hl=zh-TW&sa=X&ved=0ahUKEwj84vXliMX4AhUYqFYBHcUMAlgQPAgI'
|
|
|
+ driver.get(googleurl)
|
|
|
+ time.sleep(6)
|
|
|
+ send_kw_elmt = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
|
|
|
+ send_kw_elmt.send_keys(q)
|
|
|
+ time.sleep(3)
|
|
|
+ send_kw_elmt.send_keys(Keys.ENTER)
|
|
|
+ time.sleep(6)
|
|
|
+ elmts_title = driver.find_elements(By.XPATH, "//div[@class='CCgQ5 vCa9Yd QfkTvb MUxGbd v0nnCb']/span")
|
|
|
+ elmts_content = driver.find_elements(By.XPATH, "//div[@class='MUxGbd yDYNvb lyLwlc']")
|
|
|
+ print(len(elmts_title))
|
|
|
+ print(len(elmts_content))
|
|
|
+
|
|
|
+ for i, j in zip(elmts_title,elmts_content):
|
|
|
+ title = i.text
|
|
|
+ content = j.text
|
|
|
+
|
|
|
+ table.insert({'kw': q, 'client': client, 'title': title, 'content': content,
|
|
|
+ 'dt': datetime.datetime.now()})
|
|
|
+
|
|
|
+ db.close()
|
|
|
+
|
|
|
+
|
|
|
+def run_once(q):
|
|
|
+ global driver
|
|
|
+ result = []
|
|
|
+ user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125'
|
|
|
+ s = Service('/Users/zooeytsai/Downloads/chromedriver 4')
|
|
|
+ op = webdriver.ChromeOptions()
|
|
|
+ # options.add_argument('--headless')
|
|
|
+ # options.add_argument('--remote-debugging-port=9222')
|
|
|
+ # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
|
|
|
+ # options.add_argument("--user-agent=" + user_agent)
|
|
|
+ # options.add_argument("--incognito")
|
|
|
+ op.add_argument('--disable-dev-shm-usage')
|
|
|
+ op.add_argument('--no-sandbox')
|
|
|
+ op.add_argument(r'user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data')
|
|
|
+ op.add_argument(f"profile-directory=Profile 25")
|
|
|
+ op.add_experimental_option("excludeSwitches", ["enable-automation"])
|
|
|
+ op.add_experimental_option('useAutomationExtension', False)
|
|
|
+ driver = webdriver.Chrome(options=op,
|
|
|
+ executable_path=r'C:\Users\Administrator\Downloads\chromedriver_win32 (4)\chromedriver')
|
|
|
+
|
|
|
+ driver = webdriver.Chrome(
|
|
|
+ options=op, service=s)
|
|
|
+ str1 = driver.capabilities['browserVersion']
|
|
|
+ print('版本', str1)
|
|
|
+ driver.delete_all_cookies()
|
|
|
+ driver.set_window_size(1400, 1000)
|
|
|
+
|
|
|
+ print(q)
|
|
|
+ process_query(q)
|
|
|
+ time.sleep(3)
|
|
|
+ driver.quit()
|
|
|
+
|
|
|
+
|
|
|
+class JParams(object):
|
|
|
+
|
|
|
+ def get(self, kw, domain):
|
|
|
+ print(kw)
|
|
|
+ print(domain)
|
|
|
+ run_once((kw, domain))
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ # fire.Fire(JParams)
|
|
|
+ run_once(('新北 家具 推薦','班尼斯'))
|