淘宝模拟登陆抓取失败
# __author__ = ''
# -*- coding: utf-8 -*-
import requests
import re
s = requests.session()
login_data = {'email': 'xxx', 'password': 'xxx', }
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
'Host':'log.mmstat.com',
'Referer':'https://www.taobao.com/'
}
# post 数据实现登录
s.post('https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fwww.taobao.com%2F', login_data, headers=headers)
# 验证是否登陆成功,抓取'淘宝'首页看看内容
r = s.get('https://www.taobao.com')
print r.text
还是小白
用户名和密码省去嘞
得到的还是未登录时的代码,不知道自己少了什么,有成功的大神能告知一下咩
迷茫2017-04-17 17:34:45
注意在發送請求的時候附上cookie~
建議在模擬登陸的時候注意下面幾點:
自己在瀏覽器看一下正常登入時發出去的請求:
提交了哪些欄位?
帶了哪些cookie?
請求的位址是否有參數?
下面是我爬取雲南大學圖書館借閱資訊的爬蟲。雲端大圖書館登入系統的預設密碼是學號後八位。
那麼它在提交表單的時候不只是用戶名和密碼,還有一個隱藏的lt
字段,這個字段是生成登錄表單的時候寫在隱藏的input裡面,所以必須提取;
在post表單的地址裡面,還有一個jsessionid字段,這個也是要自己從登陸頁面提取加上的;
總之,希望我的解決思路能給你一些引導。
反正見招拆招~
當你模擬的登入資訊跟正常提交的資訊伺服器察覺不出異常的時候,就登陸成功咯~
所以,非常非常重要的一點--- 模仿正常的登入動作,在瀏覽器的network調試工具裡不斷琢磨吧。
下面附上我前一段時間寫的模擬登陸爬蟲
#coding:utf-8
#!/usr/bin/python
import HTMLParser
import urlparse
import urllib
import urllib2
import cookielib
import string
import re
import sys
import time
reload(sys)
sys.setdefaultencoding('utf-8')
COOKIE_FLAG = True # true使用这次cookie false使用上次cookie
hosturl = "http://202.203.222.202/cas/login"
history = "http://202.203.222.202/myspace/reader/book/historyBorrow?pageSize=200&pageNo=1"
cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
#获取 lt
def getHTML(stdNum):
cookie = cookielib.CookieJar()
handler=urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
response = opener.open(hosturl)
for item in cookie:
if item.name == "JSESSIONID":
jsessionId = item.value
loginHTML = response.read()
searchString = 'name="lt" value='
try:
index = loginHTML.index(searchString)
except Exception as err:
print "err:\t" + err
return False
lt = loginHTML[index +17:index +56]
# print "lt:\t" + lt
print "JD: "+jsessionId
targetPost = 'http://202.203.222.202/cas/login;jsessionid='+ jsessionId + '?service=http%3A%2F%2F202.203.222.202%2Fmyspace%2Freader%2Findex'
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1',
"Host": "202.203.222.202",
"Origin": "http://202.203.222.202",
"Referer": "http://202.203.222.202/cas/login?service=http://202.203.222.202/myspace/reader/index",
"Upgrade-Insecure-Requests": 1,
}
postData = {
"username":stdNum,
"password":stdNum[3:],
"lt": lt,
"execution": 'e1s1',
"_eventId": "submit",
"submit": "登录",
}
postData = urllib.urlencode(postData)
request = urllib2.Request(targetPost, postData, headers)
logResult = urllib2.urlopen(request).read().decode("utf8")
if "登录系统" in logResult:
print "失败啦!"
return "login failed "
historyBorrowPage = urllib2.urlopen(history)
historyHTML = historyBorrowPage.read().decode('utf8')
if "登录系统" in historyHTML:
return False
else:
logout = "http://202.203.222.202/myspace/reader/logout"
logoutYa = urllib2.Request(logout)
logoutPage = urllib2.urlopen(logoutYa)
return historyHTML