人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

python scrapy簡單模擬登錄的代碼分析

發(fā)布日期:2022-02-23 12:24 | 文章來源:CSDN

1、requests模塊。直接攜帶cookies請求頁面。

找到url,發(fā)送post請求存儲cookie。

2、selenium(瀏覽器自動處理cookie)。

找到相應(yīng)的input標簽,輸入文本,點擊登錄。

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)文章希望大家以后多多支持本站!

香港快速服務(wù)器

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部