筆者是一個癡迷于挖掘資料中的價值的學習人,希望在平日的工作學習中,挖掘資料的價值,找尋資料的秘密,筆者認為,資料的價值不僅僅隻展現在企業中,個人也可以體會到資料的魅力,用技術力量探索行為密碼,讓大資料助跑每一個人,歡迎直筒們關注我的公衆号,大家一起讨論資料中的那些有趣的事情。
我的公衆号為:livandata
1、 用fiddler擷取Cookies:
首先用自己的賬号登入到網頁版的手機微網誌上,
1) 擷取登入資料包:
Cookie={“Cookie”:“XXXXXXXXX”}
Html = requests.get(url, cookies= cookie)
當使用者輸入使用者名密碼,并點選“保持登入”以後,這些資料會被發送給伺服器,然後伺服器驗證資料,并成功登入。
需要送出的資料有哪些:
Data = {
‘Mobile’:(使用者名),
Password:(密碼),
‘Remember’:(是否保持登入),
‘backURL(登陸以後傳回的位址),
backTitle’:(登入以後傳回的标題),
‘tryCount’:(嘗試次數),
‘vk’:(一個簡單的驗證碼),
‘submit’:(登入),
‘action’:(URL參數)
}
主要是post方式送出(get方式送出時沒有具體的data form,隻需要用Cookies即可):
送出post資料時首先要建立一個data,然後将data放入到requests中:
确定要爬取的網頁,在未登入的情況下微網誌會跳轉到登入頁面,即為下方的微網誌登入界面,确定登入的url:
輸入“使用者名\密碼”之後,頁面會跳轉到需要爬取的頁面,使用“開發者工具”的network确定form data資料:
會發現password後面跟有“_2125”,搜尋源代碼可以發現“_2125”的值,由此可以明确,password_2125等字段在源代碼中可以确認。
而action中的參數為下圖中的rand:
自此,可以确認相應的代碼:
#!/usr/bin/env python
# _*_ UTF-8 _*_
import requests
from lxml import etree
from multiprocessing.dummy import Pool
url = 'http://weibo.cn/u/1890493665'
url_login = 'https://.weibo.cn/login/'
html = requests.get(url, cookies =cook).content.decode('gbk')
selector = etree.HTML(html)
print(html)
從源碼中确認出password的關鍵字怎麼用:
password = selector.xpath()[]
vk = selector.xpath()[]
action = selector.xpath()[]
print(action)
print(password)
print(vk)
new_url = url_login + action
data = {
'mobile':'[email protected]',
password:'xujingboyy123',
'remember':'on',
'backURL':'http://weibo.cn/u/1890493665',
'backTitle':u'微網誌',
'tryCount':'',
'vk':vk,
'submit':u'登入'
}
new_html = requests.post(new_url, data=data).content
new_selector = etree.HTML(new_html)
content = new_selector.xpath()
for each in content:
text = each.xpath('string(.)')
b=1
print(text)