爬取githubs——登入後的東西(兩種方法)
注意:scrapy也是能傳送post請求的,但是不使用scrapy.Request,而是要使用scrapy.FormRequest
1.開啟githubs,輸入https://github.com/login來到登入頁面,點選檢查,
分析:network裡看到data要攜帶有的資料是commit,utf8,authenticity_token,login,password
,
![11616627-4c19e43280f28fba.png](https://i.iter01.com/images/43d78b94ec35b0a8fe0dc86904f4dea3d033674122673c7806f27d7891f3f6ea.png)
3.png
2.在githubs的登入頁面,找到commit,utf8,authenticity_token,在element中的位置,並用xpath找出來
![11616627-31e2edd5f9f8be3b.png](https://i.iter01.com/images/088a0a65e5654a49ae6c001dc75fdfdbed0cb43f07314c25c4481a9fc52c8d7b.png)
2.png
上圖所示authenticity_token的位置:
//input[@name='authenticity_token']/@value
![11616627-dc187a927eb26960.png](https://i.iter01.com/images/b710cbed20ab94b1aa42f8c0b4ebd516ac1237f5dca2d6495bb9aa8ffd946975.png)
1.png
上圖所示:utf8所在的位置:
//input[@name='utf8']/@value
同理:commit的位置:
//input[@name='commit']/@value
附上登入spider的原始碼:
# -*- coding: utf-8 -*-
import re
import scrapy
class GhSpider(scrapy.Spider):
name = 'gh'
allowed_domains = ['github.com']
start_urls = ['http://github.com/login']
def parse(self, response):
authenticity_token = response.xpath("//input[@name='authenticity_token']/@value").extract_first()
utf8 = response.xpath("//input[@name='utf8']/@value").extract_first()
commit = response.xpath("//input[@name='commit']/@value").extract_first()
post_data = dict(
commit=commit,
utf8=utf8,
authenticity_token=authenticity_token,
login="你的使用者名稱",
password="你的githubs的密碼"
)
yield scrapy.FormRequest(
"https://github.com/session",
formdata=post_data,
callback=self.after_login
)
def after_login(self,response):
print(re.findall("ycw",response.body.decode()))
with open("a.html","w",encoding="utf-8")as f:
f.write(response.body.decode())
if __name__ == '__main__':
from scrapy import cmdline
cmdline.execute("scrapy crawl gh".split())
想對登入後的頁面進行提取還是別的處理,在def after_login()後面繼續寫程式碼
即使密碼輸入錯誤,使用re也能匹配到部分使用者名稱,可以將html儲存下來,檢視結果
with open("a.html","w",encoding="utf-8")as f:
f.write(response.body.decode())
方法二:
form表單只有一個的時候,我們可以使用如下方式,自動的從response中尋找from表單
yield scrapy.FormRequest.from_response(
response, #自動的從response中尋找from表單
![11616627-32728479f999d628.png](https://i.iter01.com/images/2526d45910414219387a74960ad5c5804b68ad2a7d8cbfc848a99c6d88e02f72.png)
1.png
建立專案
D:\scrapy_1>cd login1
D:\scrapy_1\login1>scrapy genspider gt2 github.com
# -*- coding: utf-8 -*-
import re
import scrapy
class Gt2Spider(scrapy.Spider):
name = 'gt2'
allowed_domains = ['github.com']
start_urls = ['http://github.com/login']
def parse(self, response):
yield scrapy.FormRequest.from_response(
response, #自動的從response中尋找from表單
formdata={"login":"你的使用者名稱","password":"你的githubs的密碼"},
callback=self.after_login
)
def after_login(self,response):
print(re.findall("ycw",response.body.decode()))
執行:
scrapy crawl gt2
程式碼執行結果
![11616627-b0f85c04dc4bb374.png](https://i.iter01.com/images/3ef857849ef06e4fb761b2a48e9506194392aee42af4ce81ac1c96ed283358c0.png)
2.png
方法三:
攜帶cookie的方式
相關文章
- Python 爬取網頁資料的兩種方法Python網頁
- 破解「登入後複製」的三種方法
- 為爬蟲獲取登入cookies:登入的恩恩怨怨爬蟲Cookie
- Python爬蟲教程-13-爬蟲使用cookie爬取登入後的頁面(人人網)(下)Python爬蟲Cookie
- Python爬蟲教程-12-爬蟲使用cookie爬取登入後的頁面(人人網)(上)Python爬蟲Cookie
- uniapp 完成兩種方式登入 驗證碼登入 密碼登入APP密碼
- vnc批次登入,2種VNC批次登入Linux的方法VNCLinux
- 如何用Python爬取需要登入的網站?Python網站
- 快速爬取登入網站資料網站
- JS讀取本地TXT文字的兩種方法JS
- Python爬蟲的兩套解析方法和四種爬蟲實現Python爬蟲
- 爬取資料時防止爬蟲被限制的四種方法爬蟲
- 操作教程|在 MeterSphere 中透過 SSH 登入伺服器的兩種方法伺服器
- 為爬蟲獲取登入cookies:charles工具的使用爬蟲Cookie
- 獲取爬蟲動態IP的三種方法爬蟲
- 爬蟲兩種繞過5s盾的方法爬蟲
- Python爬蟲入門【4】:美空網未登入圖片爬取Python爬蟲
- Ubuntu設定root登入有兩種方式Ubuntu
- APP爬蟲-雙向認證抓包的兩種方法APP爬蟲
- Python 爬蟲模擬登入方法彙總Python爬蟲
- 爬蟲實戰(二):Selenium 模擬登入並爬取資訊爬蟲
- 如何解決網站登入後反爬的問題?網站
- python兩種獲取剪貼簿內容的方法Python
- Python獲取list中指定元素索引的兩種方法Python索引
- 為爬蟲獲取登入cookies: 使用Charles和requests模擬微博登入爬蟲Cookie
- 從HDFS的寫入和讀取中,我發現了點東西
- Python爬蟲實戰之(四)| 模擬登入京東商城Python爬蟲
- Python爬蟲入門教程 4-100 美空網未登入圖片爬取Python爬蟲
- python爬蟲實戰:爬取西刺代理的代理ip(二)Python爬蟲
- 為爬蟲獲取登入cookies:使用萬能鑰匙 Selenium 搞定一切登入爬蟲Cookie
- Android兩種簡單的載入GIF圖片的方法Android
- 亂序的兩種方法
- 10,函式和方法相關的東西函式
- 提升爬蟲效率的兩大方法爬蟲
- 為爬蟲獲取登入cookies: 使用browsercookie從瀏覽器獲取cookies爬蟲Cookie瀏覽器
- 用python寫一個豆瓣短評通用爬蟲(登入、爬取、視覺化)Python爬蟲視覺化
- sqlplus 命令登入 Oracle資料庫的多種方法DXNASQLOracle資料庫
- 《燕雲十六聲》“在玩一種很新的東西”