python 爬蟲——登入知乎
python 爬蟲——登入知乎
說明
語言,版本,包:
pytho3.4
requests.get
request.post
requests.session
步驟:
1 通過瀏覽器f12開發者工具,找到登入時提交的表單以及請求的url
注意請求的url不是瀏覽器域名框的url
因為知乎改了,現在不需要驗證碼了,所以圖片中少了驗證碼的引數,不過程式碼裡是有的
2 設計到登入等會跳轉的頁面,推薦使用火狐瀏覽器,然後開啟持續日誌功能
3具體實現見程式碼
程式碼:
__author__ = 'Guo'
# coding:utf-8
import requests
from bs4 import BeautifulSoup
# 解決編碼問題
try:
from io import BytesIO as StringIO
except ImportError:
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
def login():
header = {
'Host':"www.zhihu.com",
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0",
'Accept':"*/*",
'Accept-Language':"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
'Accept-Encoding':"gzip, deflate",
'Content-Type':"application/x-www-form-urlencoded; charset=UTF-8"
}
session = requests.session() # 處理cookie
res = session.get('http://www.zhihu.com/captcha.gif',headers = header) # seesionh會記下訪問驗證碼的cookie
with open('captcha.jpg', 'wb') as fp:
fp.write(res.content) # 講驗證碼圖片儲存在本地。
# print(res.content)
#print(res.cookies)
# cook = res.cookies
capt = input('input captcha') # 手動讀取
xsrf = BeautifulSoup(session.get('http://www.zhihu.com/#signin').content).find('input', attrs={'name': '_xsrf'})['value'] # 從原始碼中獲取的表單中的一個欄位
print(xsrf)
data = {
'_xsrf': xsrf,
'email': '*******@gmail.com',
'password': '*******',
'remember_me': 'true',
'captcha': capt
}
response = session.post('http://www.zhihu.com/login/email',data = data,headers = header ) # post 登入時提交的表單,此時session中是儲存了此時的cookie 的
print(response.status_code)
print(response.content.decode('utf-8'))
html = session.get('http://www.zhihu.com/#signi') # 再次訪問,由於cookie 的存在,記下了登入的狀態,所以此時就可以獲取我們登入之後的類容了啦。
print(html.content.decode('utf-8'))
if __name__ == '__main__':
login()
相關文章
- python爬蟲如何爬知乎的話題?Python爬蟲
- Python爬蟲新手教程: 知乎文章圖片爬取器Python爬蟲
- 新手爬蟲教程:Python爬取知乎文章中的圖片爬蟲Python
- python爬蟲專案(新手教程)之知乎(requests方式)Python爬蟲
- Python爬蟲抓取知乎所有使用者資訊Python爬蟲
- python爬蟲如何用session保持登入?Python爬蟲Session
- Python 爬蟲模擬登入方法彙總Python爬蟲
- Python 爬蟲 + 人臉檢測 —— 知乎高顏值圖片抓取Python爬蟲
- Python網路爬蟲實戰:爬取知乎話題下 18934 條回答資料Python爬蟲
- Python爬蟲入門【4】:美空網未登入圖片爬取Python爬蟲
- 分散式爬蟲之知乎使用者資訊爬取分散式爬蟲
- python 爬蟲網頁登陸Python爬蟲網頁
- 分散式爬蟲很難嗎?用Python寫一個小白也能聽懂的分散式知乎爬蟲分散式爬蟲Python
- Python爬蟲實戰之(五)| 模擬登入wechatPython爬蟲
- Python爬蟲入門Python爬蟲
- Python爬蟲教程-13-爬蟲使用cookie爬取登入後的頁面(人人網)(下)Python爬蟲Cookie
- Python爬蟲教程-12-爬蟲使用cookie爬取登入後的頁面(人人網)(上)Python爬蟲Cookie
- Python分散式爬蟲(三) - 爬蟲基礎知識Python分散式爬蟲
- Python爬蟲之路-爬蟲基礎知識(理論)Python爬蟲
- 2019年最新 Python 模擬登入知乎 支援驗證碼Python
- 什麼是Python爬蟲?python爬蟲入門難嗎?Python爬蟲
- 【爬蟲】python爬蟲從入門到放棄爬蟲Python
- [Python]爬蟲獲取知乎某個問題下所有圖片並去除水印Python爬蟲
- python-爬蟲入門Python爬蟲
- Python爬蟲入門教程 4-100 美空網未登入圖片爬取Python爬蟲
- Python爬蟲入門教程 50-100 Python3爬蟲爬取VIP視訊-Python爬蟲6操作Python爬蟲
- 【Python學習】爬蟲爬蟲爬蟲爬蟲~Python爬蟲
- GitHub 熱門:各大網站的 Python 爬蟲登入彙總Github網站Python爬蟲
- Python爬蟲實戰之(四)| 模擬登入京東商城Python爬蟲
- 為什麼學習python及爬蟲,Python爬蟲[入門篇]?Python爬蟲
- 為爬蟲獲取登入cookies:登入的恩恩怨怨爬蟲Cookie
- 不踩坑的Python爬蟲:Python爬蟲開發與專案實戰,從爬蟲入門 PythonPython爬蟲
- 爬蟲入門基礎-Python爬蟲Python
- python3 爬蟲入門Python爬蟲
- 用python寫一個豆瓣短評通用爬蟲(登入、爬取、視覺化)Python爬蟲視覺化
- Python爬蟲入門,8個常用爬蟲技巧盤點Python爬蟲
- Python爬蟲學習線路圖丨Python爬蟲需要掌握哪些知識點Python爬蟲
- 爬蟲模擬登入—OAUTH的詳解爬蟲OAuth
- python爬蟲---網頁爬蟲,圖片爬蟲,文章爬蟲,Python爬蟲爬取新聞網站新聞Python爬蟲網頁網站