selenium 不断切换代理 打淘宝小店流量

来源:互联网 发布:复杂网络 大数据 编辑:程序博客网 时间:2024/04/28 16:28

写个脚本,模拟浏览器搜索商品,并不断打流量。

在centos 6.2上测试通过。

#-*- coding:utf-8 –*-import seleniumfrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byimport timeimport datetimedef getSearchURL():    urlPrefix = ('http://s.taobao.com/search?q=%B5%C2%B9%FA%B0%AE%CB%FB%C3%C03%B6%CE&commend=all&search_type=item&sourceId=tb.index&spm=1.1000386.5803581.d4908513')    urlPostfix_t = "&initiative_id=tbindexz_%s"    today = datetime.date.today()    datestr = today.strftime('%Y%m%d')    urlPostfix = urlPostfix_t % datestr    url = urlPrefix + urlPostfix    return urlPROXY_HOST = "61.242.169.94"PROXY_PORT = 81 fp = webdriver.FirefoxProfile() # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5fp.set_preference("network.proxy.type", 1) fp.set_preference("network.proxy.http", PROXY_HOST)fp.set_preference("network.proxy.http_port", PROXY_PORT)fp.set_preference("network.proxy.ftp", PROXY_HOST)fp.set_preference("network.proxy.ftp_port", PROXY_PORT)fp.set_preference("network.proxy.ssl", PROXY_HOST)fp.set_preference("network.proxy.ssl_port", PROXY_PORT) fp.set_preference("network.proxy.no_proxies_on", "") # set this value as desired driver = webdriver.Firefox()#driver = webdriver.Firefox(firefox_profile=fp)driver.implicitly_wait(10)#driver.get('http://www.ip138.com/')url = getSearchURL()driver.get(url)elem = Nonewhile 1:    try:            time.sleep(0.2)        elem = driver.find_element_by_link_text("fanwb1985")    except selenium.common.exceptions.NoSuchElementException :        elem = driver.find_element_by_link_text("下一页")        elem.click()        continue    if elem:        elem.click()        breakfindStr = '''t'''links = driver.find_elements_by_partial_link_text(findStr)linksH= []for link in links:    #print link.get_attribute("href")    linka= link.get_attribute("href")    linksH.append(linka)print linksH'''foo = driver.find_element_by_tag_name("a")for element in foo:    link = element.getAttribute("src")    if str(link).find(findStr)!= -1:        links.append(link)links = list(set(links))print links'''driver.close()    


0 0
原创粉丝点击