from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import urllib2
from bs4 import BeautifulSoup
import requests
import random
#设置代理IP
def get_ip_random():
url = 'http://www.xicidaili.com/nn/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}
web_data = requests.get(url, headers=headers)
soup = BeautifulSoup(web_data.text, 'lxml')
ips = soup.find_all('tr')
ip_list = []
for i in range(1, len(ips)):
ip_info = ips[i]
tds = ip_info.find_all('td')
ip_list.append(tds[1].text + ':' + tds[2].text)
proxy_list = []
for ip in ip_list:
proxy_list.append('http://' + ip)
proxy_ip = random.choice(proxy_list)
proxies = {'http': proxy_ip}
return proxies
def login(uname, pwd):
#urllib2.ProxyHandler({'http':'http://120.193.146.97:843'})
urllib2.ProxyHandler(get_ip_random())
driver = webdriver.Firefox()
driver.maximize_window() #将浏览器最大化显示
driver.delete_all_cookies()
driver.get("https://login.taobao.com/member/login.jhtml")
#load the switch
element=WebDriverWait(driver,60).until(lambda driver :
driver.find_element_by_xpath("//*[@id='J_Quick2Static']"))
element.click()
driver.implicitly_wait(20)
username=driver.find_element_by_name("TPL_username")
if not username.is_displayed:
driver.implicitly_wait(20)
driver.find_element_by_xpath("//*[@id='J_Quick2Static']").click()
driver.implicitly_wait(20)
username.send_keys(uname)
driver.find_element_by_name("TPL_password").send_keys(pwd)
driver.implicitly_wait(20)
driver.find_element_by_xpath("//*[@id='J_SubmitStatic']").click()
time.sleep(4)
select = driver.find_element_by_xpath('//*[@id="tp-bought-root"]')
select.click()
return driver
login("用户名",'密码')
利用Python模拟淘宝自动登录
最新推荐文章于 2024-06-15 16:53:09 发布