免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

scrapy模擬登錄代碼-創(chuàng)新互聯(lián)

本文章向大家介紹scrapy模擬登錄代碼,主要包括{**}的使用實(shí)例,應(yīng)用技巧,基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。

成都創(chuàng)新互聯(lián)公司是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的十余年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如航空箱等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致贊美。

在Scrapy中,模擬登陸網(wǎng)站一般有如下兩種實(shí)現(xiàn)方式:

           (1) 請(qǐng)求時(shí)攜帶Cookies

           (2) 發(fā)送Post請(qǐng)求獲取Cookies

請(qǐng)求時(shí)攜帶Cookies

對(duì)于一些Cookies過期時(shí)間很長(zhǎng)的不規(guī)范網(wǎng)站,如果我們能夠在Cookies過期之前爬取到所有我們想要的數(shù)據(jù),可以考慮在請(qǐng)求時(shí)直接將Cookies信息帶上來模擬用戶登錄。

以下是模擬登錄Github的示例代碼:

# -*- coding: utf-8 -*-
import scrapy
import re
 
class TmallLoginSpider(scrapy.Spider):
  name = 'github_login3'
  allowed_domains = ['github.com']
  start_urls = ['https://github.com/']
 
  def start_requests(self): # 請(qǐng)求時(shí)攜帶Cookies
    cookies = '_ga=GA1.2.363045452.1554860671; tz=Asia%2FShanghai; _octo=GH1.1.1405577398.1554860677; _device_id=ee3ff12512668a1f9dc6fb33e388ea20; ignored_unsupported_browser_notice=false; has_recent_activity=1; user_session=5oxrsfsZCor1iJFCgRXXyeAXd8hcmzEUGh70-xHWLjQkT62Q; __Host-user_session_same_site=5oxrsfsZCor1iJFCgRXXyeAXd8hcmzEUGh70-xHWLjQkT62Q; logged_in=yes; dotcom_user=pengjunlee; _gat=1'
    cookies = {i.split('=')[0]: i.split('=')[1] for i in cookies.split('; ')}
    yield scrapy.Request(self.start_urls[0], cookies=cookies)
    
  def parse(self, response): # 驗(yàn)證是否請(qǐng)求成功
    print(re.findall('Learn Git and GitHub without any code!',response.body.decode()))

執(zhí)行爬蟲后,后臺(tái)部分日志截圖如下:

scrapy模擬登錄代碼

發(fā)送Post請(qǐng)求模擬登錄

Scrapy還提供了兩種通過發(fā)送Post請(qǐng)求來獲取Cookies的方法。

scrapy.FormRequest()

使用scrapy.FormRequest()發(fā)送Post請(qǐng)求實(shí)現(xiàn)模擬登陸,需要人為找出登錄請(qǐng)求的地址以及構(gòu)造出登錄時(shí)所需的請(qǐng)求數(shù)據(jù)。

使用scrapy.FormRequest()模擬登錄Github的示例代碼:

# -*- 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()))

從后臺(tái)日志不難看出,Scrapy 在請(qǐng)求完 https://github.com/session 后,自動(dòng)幫我們重定向到了Github首頁(yè)。

scrapy模擬登錄代碼

scrapy.FormRequest.from_response()

scrapy.FormRequest.from_response()使用起來比 scrapy.FormRequest()更加簡(jiǎn)單方便,我們通常只需要提供用戶相關(guān)信息(賬戶和密碼)即可,scrapy.FormRequest.from_response()將通過模擬點(diǎn)擊為我們填充好其他的表單字段并提交表單。

使用scrapy.FormRequest.from_response()模擬登錄Github的示例代碼:

# -*- coding: utf-8 -*-
import scrapy
import re
 
class GithubLogin2Spider(scrapy.Spider):
  name = 'github_login2'
  allowed_domains = ['github.com']
  start_urls = ['https://github.com/login']
 
  def parse(self, response): # 發(fā)送Post請(qǐng)求獲取Cookies
    form_data = {
      'login': 'pengjunlee@163.com',
      'password': '123456'
    }
    yield scrapy.FormRequest.from_response(response,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()))

scrapy.FormRequest.from_response()方法還可以傳入其他參數(shù)來幫我們更加精確的指定表單元素:

'''
response (Response object) – 包含表單HTML的響應(yīng),將用來對(duì)表單的字段進(jìn)行預(yù)填充
formname (string) – 如果設(shè)置了該值,name 等于該值的表單將被使用
formid (string) – 如果設(shè)置了該值,id 等于該值的表單將被使用
formxpath (string) – 如果設(shè)置了該值,匹配該 xpath 的第一個(gè)表單將被使用
formcss (string) – 如果設(shè)置了該值,匹配該 css選擇器的第一個(gè)表單將被使用
formnumber (integer) – 索引值等于該值的表單將被使用,默認(rèn)第一個(gè)(索引值為 0 )
formdata (dict) – 傳入的表單數(shù)據(jù),將覆蓋form 元素中已經(jīng)存在的值
clickdata (dict) – 用于查找可點(diǎn)擊控件的屬性值
dont_click (boolean) – 如果設(shè)置為 True,將不點(diǎn)擊任何元素,直接提交表單數(shù)據(jù)
'''

以上就是小編為大家?guī)淼膕crapy模擬登錄代碼的全部?jī)?nèi)容了,希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,!

文章題目:scrapy模擬登錄代碼-創(chuàng)新互聯(lián)
地址分享:http://newbst.com/article46/dggphg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管軟件開發(fā)網(wǎng)站內(nèi)鏈網(wǎng)站收錄網(wǎng)站設(shè)計(jì)公司建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化