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 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 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: if 'hhh.com.tw' in href: # if 'searchome.net' in href: webdriver.ActionChains(driver).move_to_element(elmt).perform() webdriver.ActionChains(driver).move_to_element(elmt).click().perform() break # fw=codecs.open('c:/tmp/gg.html','w','utf-8') # fw.write(driver.page_source) # fw.close() # driver.save_screenshot("c:/tmp/screen.png") # os.startfile("c:/tmp/screen.png") # driver.quit() #qlist=['上築空間設計','豐聚室內裝修','富億空間設計','浩室設計','京璽國際','IS國際設計','優尼客','摩登雅舍','亞維空間設計','比沙列','富億設計','豐聚設計'] #qlist=['IS國際設計'] qlist=['富億空間','幸福空間節目 富億','幸福空間設計師 富億','幸福空間 富億','幸福空間 fe設計','老屋 富億','老屋翻新 富億','天井設計案例 富億','IS國際設計','豐聚設計','浩室設計','優尼客','摩登雅舍','亞維空間設計','比沙列','京璽國際','上築空間設計'] driver = webdriver.Remote( # command_executor='http://127.0.0.1:4444/wd/hub', #command_executor='http://192.53.174.202:4444/wd/hub', command_executor='http://dev2.choozmo.com:14444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME) driver.set_window_size(1400,1000) while True: q=random.choice(qlist) # q=qlist[0] print(q) process_query(q) send_msg(q) intsleep=random.randint(19,30) time.sleep(intsleep)