python scrapy簡單模擬登錄的代碼分析
1、requests模塊。直接攜帶cookies請求頁面。
找到url,發(fā)送post請求存儲cookie。
2、selenium(瀏覽器自動處理cookie)。
找到相應(yīng)的input標(biāo)簽,輸入文本,點擊登錄。
3、scrapy直接帶cookies。
找到url,發(fā)送post請求存儲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請求獲取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): # 驗證是否請求成功
print(re.findall('Learn Git and GitHub without any code!', response.body.decode()))
知識點擴展:
parse_login方法是提交完表單后callback回調(diào)函數(shù)指定要執(zhí)行的方法,為了驗證是否成功。這里我們直接在response中搜索Welcome Liu這個字眼就證明登錄成功。
這個好理解,重點是yield from super().start_resquests(),這個代表著如果一旦登錄成功后,就直接帶著登錄成功后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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spark: RDD與DataFrame之間的相互轉(zhuǎn)換方法
今天小編就為大家分享一篇spark: RDD與DataFrame之間的相互轉(zhuǎn)換方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python監(jiān)聽剪切板實現(xiàn)方法代碼實例
這篇文章主要介紹了Python監(jiān)聽剪切板實現(xiàn)方法代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11
Python使用Beets模塊實現(xiàn)自動整理音樂庫
Beets是一個功能強大的Python庫,用于處理音樂文件的元數(shù)據(jù),在本文中,我們將探討beets模塊的常見使用方法,感興趣的可以跟隨小編一起學(xué)習(xí)一下2024-03-03
python將matplotlib嵌入到tkinter中的步驟詳解
tkinter是Python標(biāo)準(zhǔn)庫中自帶的GUI工具,使用十分方便,如能將matplotlib嵌入到tkinter中,就可以做出相對專業(yè)的數(shù)據(jù)展示系統(tǒng),很有競爭力,本文就給大家介紹python將matplotlib嵌入到tkinter中的方法步驟,需要的朋友可以參考下2023-08-08
Python數(shù)據(jù)分析中常見統(tǒng)計方法詳解
數(shù)據(jù)分析是現(xiàn)代社會中不可或缺的一部分,通過對數(shù)據(jù)的統(tǒng)計和分析,我們可以得出有用的信息和見解,本文將介紹在?Python?中常見的數(shù)據(jù)統(tǒng)計方法,希望對大家有所幫助2024-02-02
python基于urllib實現(xiàn)按照百度音樂分類下載mp3的方法
這篇文章主要介紹了python基于urllib實現(xiàn)按照百度音樂分類下載mp3的方法,涉及Python使用urllib模塊操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05
python實現(xiàn)基本進(jìn)制轉(zhuǎn)換的方法
這篇文章主要介紹了python實現(xiàn)基本進(jìn)制轉(zhuǎn)換的方法,涉及Python數(shù)學(xué)運算的取余與字符串操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07

