Scrapy框架實現(xiàn)的登錄網(wǎng)站操作示例
本文實例講述了Scrapy框架實現(xiàn)的登錄網(wǎng)站操作。分享給大家供大家參考,具體如下:
一、使用cookies登錄網(wǎng)站
import scrapy
class LoginSpider(scrapy.Spider):
name = 'login'
allowed_domains = ['xxx.com']
start_urls = ['https://www.xxx.com/xx/']
cookies = ""
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url, cookies=self.cookies, callback=self.parse)
def parse(self, response):
with open("01login.html", "wb") as f:
f.write(response.body)
二、發(fā)送post請求登錄, 要手動解析網(wǎng)頁獲取登錄參數(shù)
import scrapy
class LoginSpider(scrapy.Spider):
name='login_code'
allowed_domains = ['xxx.com']
#1. 登錄頁面
start_urls = ['https://www.xxx.com/login/']
def parse(self, response):
#2. 代碼登錄
login_url='https://www.xxx.com/login'
formdata={
"username":"xxx",
"pwd":"xxx",
"formhash":response.xpath("http://input[@id='formhash']/@value").extract_first(),
"backurl":response.xpath("http://input[@id='backurl']/@value").extract_first()
}
#3. 發(fā)送登錄請求post
yield scrapy.FormRequest(login_url, formdata=formdata, callback=self.parse_login)
def parse_login(self, response):
#4.訪問目標(biāo)頁面
member_url="https://www.xxx.com/member"
yield scrapy.Request(member_url, callback=self.parse_member)
def parse_member(self, response):
with open("02login.html",'wb') as f:
f.write(response.body)
三、發(fā)送post請求登錄, 自動解析網(wǎng)頁獲取登錄參數(shù)
import scrapy
class LoginSpider(scrapy.Spider):
name='login_code2'
allowed_domains = ['xxx.com']
#1. 登錄頁面
start_urls = ['https://www.xxx.com/login/']
def parse(self, response):
#2. 代碼登錄
login_url='https://www.xxx.com/login'
formdata={
"username":"xxx",
"pwd":"xxx"
}
#3. 發(fā)送登錄請求post
yield scrapy.FormRequest.from_response(
response,
formxpath="http://*[@id='login_pc']",
formdata=formdata,
method="POST", #覆蓋之前的get請求
callback=self.parse_login
)
def parse_login(self, response):
#4.訪問目標(biāo)頁面
member_url="https://www.xxx.com/member"
yield scrapy.Request(member_url, callback=self.parse_member)
def parse_member(self, response):
with open("03login.html",'wb') as f:
f.write(response.body)
更多相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家基于Scrapy框架的Python程序設(shè)計有所幫助。
相關(guān)文章
Python3.x+pyqtgraph實現(xiàn)數(shù)據(jù)可視化教程
這篇文章主要介紹了Python3.x+pyqtgraph實現(xiàn)數(shù)據(jù)可視化教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
跟老齊學(xué)Python之?dāng)?shù)據(jù)類型總結(jié)
前面已經(jīng)洋洋灑灑地介紹了不少數(shù)據(jù)類型。不能再不顧一切地向前沖了,應(yīng)當(dāng)總結(jié)一下。這樣讓看官能夠從總體上對這些數(shù)據(jù)類型有所了解,如果能夠有一覽眾山小的感覺,就太好了。2014-09-09
Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法
這篇文章主要介紹了Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動態(tài)圖)
這篇文章主要介紹了Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動態(tài)圖),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
python 如何用terminal輸入?yún)?shù)
這篇文章主要介紹了python 如何用terminal輸入?yún)?shù)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
Pytorch-Geometric中的Message?Passing使用及說明
這篇文章主要介紹了Pytorch-Geometric中的Message?Passing使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12

