selenium+chromedriver刷点击量
#coding=utf-8 import re import time import json import requests from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC URL = u'www.baidu.com' def get_http_proxy_from_pool(): proxy = {} try: url = "xxxxxxxxxxxxxxxxxxxxxxxxxx" response = requests.get(url) jsonstr = response.text result = json.loads(jsonstr) if result.get("code") == 1: proxy_list = result.get("data") proxy_item = proxy_list[0] proxy = {"http":"http://%s:%s"%(proxy_item.get("proxy_ip"),proxy_item.get("proxy_port"))} else: raise Exception("failed to get http proxy.") except Exception as e: print str(e) finally: return proxy def get_desired_capabilities(): proxy = get_http_proxy_from_pool().get(u"http") print u"当前代理: %s" % proxy options = webdriver.ChromeOptions() desired_capabilities = options.to_capabilities() desired_capabilities[u'proxy'] = { u"httpProxy": proxy, u"ftpProxy": proxy, u"sslProxy": proxy, u"noProxy": None, u"proxyType": u"MANUAL", u"class": u"org.openqa.selenium.Proxy", u"autodetect": False} return desired_capabilities def get_chrome_browser(): desired_capabilities = get_desired_capabilities() browser = webdriver.Chrome(desired_capabilities = desired_capabilities) return browser def get_headless_chrome_browser(): chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('headless') desired_capabilities = get_desired_capabilities() browser = webdriver.Chrome(desired_capabilities = desired_capabilities, chrome_options=chrome_options) return browser def search_keyword(keyword): browser = get_headless_chrome_browser() #设置超时时间 wait = WebDriverWait(browser, 5) try: browser.get(URL) #等待搜索框与搜索按钮加载完成 input = wait.until(EC.presence_of_element_located((By.ID, u'searchKey'))) input.send_keys(keyword) button = wait.until(EC.element_to_be_clickable((By.ID, u'btn-search'))) button.click() print u"%s搜索成功" % keyword except Exception as e: print(u'搜索过程出错') finally: browser.delete_all_cookies() browser.close() def main(): results = get_company_from_mysql() for result in results: keyword = result[0] if result else "" print keyword search_keyword(keyword) time.sleep(1) if __name__=='__main__': main()