Python模拟淘宝登录的两种⽅法
⽅法⼀、urllib的post登录
import urllib
import urllib2
import cookielib
def taobao(username,password):
cj = cookielib.CookieJar()
print cj
post_data = urllib.urlencode(
{
'TPL_password':password,
'TPL_username':username,
})
path = 'login.taobao/member/login.jhtml'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13')] urllib2.install_opener(opener)
req = urllib2.Request(path,post_data)
#try login
conn = urllib2.urlopen(req)
html = ad().decode('gbk','ignore')
selenium获取cookieprint cj
print html
taobao('username','password')
print'OK'
⽅法⼆:通过selenium模拟浏览器登录
from selenium import webdriver
driver = webdriver.Chrome()
#driver = webdriver.Firefox()
<('login.taobao/member/login.jhtml')
driver.find_element_by_xpath('//*[@id="J_QRCodeLogin"]/div[5]/a[1]').click()
driver.find_element_by_id("TPL_username_1").clear()
driver.find_element_by_id("TPL_password_1").clear()
driver.find_element_by_id("TPL_username_1").send_keys('xxx')
driver.find_element_by_id("TPL_password_1").send_keys('xxx')
driver.find_element_by_id("J_SubmitStatic").click()
#_cookies()取得cookie
cookie = "; ".join([item["name"] + "=" + item["value"] +"\n"for item _cookies()])
print cookie
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论