python scrapy簡單模擬登錄的代碼分析
1、requests模塊。直接攜帶cookies請(qǐng)求頁面。
找到url,發(fā)送post請(qǐng)求存儲(chǔ)cookie。
2、selenium(瀏覽器自動(dòng)處理cookie)。
找到相應(yīng)的input標(biāo)簽,輸入文本,點(diǎn)擊登錄。
3、scrapy直接帶cookies。
找到url,發(fā)送post請(qǐng)求存儲(chǔ)cookie。
# -*- coding: utf-8 -*- import scrapy import re class GithubLoginSpider(scrapy.Spider): name = 'github_login' allowed_domains = ['github.com'] start_urls = ['https://github.com/login'] def parse(self, response): # 發(fā)送Post請(qǐng)求獲取Cookies 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() form_data = { 'login': 'pengjunlee@163.com', 'password': '123456', 'webauthn-support': 'supported', 'authenticity_token': authenticity_token, 'utf8': utf8, 'commit': commit} yield scrapy.FormRequest("https://github.com/session", formdata=form_data, callback=self.after_login) def after_login(self, response): # 驗(yàn)證是否請(qǐng)求成功 print(re.findall('Learn Git and GitHub without any code!', response.body.decode()))
知識(shí)點(diǎn)擴(kuò)展:
parse_login方法是提交完表單后callback回調(diào)函數(shù)指定要執(zhí)行的方法,為了驗(yàn)證是否成功。這里我們直接在response中搜索Welcome Liu這個(gè)字眼就證明登錄成功。
這個(gè)好理解,重點(diǎn)是yield from super().start_resquests(),這個(gè)代表著如果一旦登錄成功后,就直接帶著登錄成功后Cookie值,方法start_urls里面的地址。
這樣的話登錄成功后的response可以直接在parse里面寫。
# -*- coding: utf-8 -*- import scrapy from scrapy import FormRequest,Request class ExampleLoginSpider(scrapy.Spider): name = "login_" allowed_domains = ["example.webscraping.com"] start_urls = ['http://example.webscraping.com/user/profile'] login_url = 'http://example.webscraping.com/places/default/user/login' def parse(self, response): print(response.text) def start_requests(self): yield scrapy.Request(self.login_url,callback=self.login) def login(self,response): formdata = { 'email':'liushuo@webscraping.com','password':'12345678'} yield FormRequest.from_response(response,formdata=formdata, callback=self.parse_login) def parse_login(self,response): # print('>>>>>>>>'+response.text) if 'Welcome Liu' in response.text: yield from super().start_requests()
到此這篇關(guān)于python scrapy簡單模擬登錄的代碼分析的文章就介紹到這了,更多相關(guān)python scrapy模擬登錄的方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
10個(gè)python3常用排序算法詳細(xì)說明與實(shí)例(快速排序,冒泡排序,桶排序,基數(shù)排序,堆排序,希爾排序,歸并排序,計(jì)數(shù)排
這篇文章主要介紹了10個(gè)python3常用排序算法詳細(xì)說明與實(shí)例,需要的朋友可以參考下2020-03-03python庫構(gòu)建之pyproject.toml配置文件詳解
pyproject.toml是Python項(xiàng)目標(biāo)準(zhǔn)化配置文件,由PEP?518引入,用于定義構(gòu)建系統(tǒng)、項(xiàng)目元數(shù)據(jù)和依賴管理,它替代了傳統(tǒng)的setup.cfg文件,通過指定構(gòu)建工具如setuptools或poetry,管理項(xiàng)目依賴,配置工具行為等,需要的朋友可以參考下2024-09-09python數(shù)據(jù)持久存儲(chǔ) pickle模塊的基本使用方法解析
這篇文章主要介紹了python數(shù)據(jù)持久存儲(chǔ) pickle模塊的基本使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實(shí)例
今天小編就為大家分享一篇python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python?類中定義多個(gè)構(gòu)造器方法重載與泛方法
這篇文章主要為大家介紹了Python?類中定義多個(gè)構(gòu)造器方法重載與泛方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解
這篇文章主要介紹了python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解,需要的朋友可以參考下2020-02-02PyCharm中鼠標(biāo)懸停在函數(shù)上時(shí)顯示函數(shù)和幫助的解決方法
這篇文章主要介紹了PyCharm中鼠標(biāo)懸停在函數(shù)上時(shí)顯示函數(shù)和幫助,本文給大家分享問題解決方法,對(duì)PyCharm鼠標(biāo)懸停函數(shù)上顯示函數(shù)的解決方法感興趣的朋友跟隨小編一起看看吧2022-11-11