import dataset from selenium import webdriver import traceback import time from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import pymysql pymysql.install_as_MySQLdb() path = 'C:\portable\chromedriver' path_z = '/Users/zooeytsai/Downloads/chromedriver 4' def restart_browser(): options = webdriver.ChromeOptions() # options.add_argument("user-agent=%s" % rua()) options.add_argument('--headless') driver=webdriver.Chrome(options=options,executable_path=path_z) driver.set_window_size(950, 20000) return driver def process_one(kw,domain): # db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4') db_local = dataset.connect('mysql://root:jondae350@localhost/ig_tags') table=db_local['google_rank'] driver=restart_browser() # escaped_search_term=urllib.parse.quote(term) googleurl = 'https://www.google.com/?num=100' 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(kw) time.sleep(3) send_kw_elmt.send_keys(Keys.ENTER) time.sleep(6) elmts=driver.find_elements_by_xpath("//div[@class='yuRUbf']/a") cnt=1 datadict={'搜尋詞':[],'結果標題':[],'結果網址':[],'結果名次':[]} for elmt in elmts: try: href=elmt.get_attribute('href') if domain in href: datadict['搜尋詞'].append(kw) datadict['結果標題'].append(elmt.text) datadict['結果網址'].append(href) datadict['結果名次'].append(str(cnt)) cnt+=1 except: print('href2 exception') traceback.print_exc() driver.quit() time.sleep(60) # db.close() db_local.close() return datadict