123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- import time
- import json
- 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.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
- def process_query(qs):
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
- table=db['general_log']
- q=qs[0]
- domain=qs[1]
- global driver
- #googleurl = 'https://www.google.com/?num=100'
- googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote(q), 100, 'zh-TW')
- driver.get(googleurl)
- time.sleep(5)
- #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)
- print(driver.current_url)
- #WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.XPATH,"//div[@class='yuRUbf']/a")))
- elmts=driver.find_elements(By.XPATH,"//div[@class='yuRUbf']//a")
- idx=1
- ranking=-1
- print('網頁數量',len(elmts))
- for elmt in elmts:
- href = elmt.get_attribute('href')
- txt = elmt.text
- if domain in href:
- print('clicked....')
- print('點擊網址', href)
- print('標題', txt)
- print("ranking", idx)
- table.insert({'kw': q, 'domain': domain, 'ranking': idx, 'title': txt, 'url': href,
- 'dt': datetime.datetime.now()})
- webdriver.ActionChains(driver).move_to_element(elmt).perform()
- webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
- time.sleep(5)
- break
- idx += 1
- db.close()
- driver.quit()
|