新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何重寫start_requests方法
說明

江干網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、響應式網(wǎng)站等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)。
1、在scrapy中,start_url是由start_requests處理的,通過重寫這種方法,start_url可以攜帶請求頭信息。
2、cookie不能放在scrapy中的headers中,在構(gòu)建請求時有專門的cookies參數(shù)。
可以接收字典形式的cookie??赡苄枰趕ettings中設置ROBOTS協(xié)議和USER_AGENT。
實例
import scrapy
class Git1Spider(scrapy.Spider):
name = 'git1'
allowed_domains = ['github.com']
start_urls = ['https://github.com/GitLqr']
def start_requests(self):
"""
重寫start_requests,發(fā)送攜帶cookies的Request。
默認start_requests只是普通的get請求,不會攜帶自定義的頭信息
"""
url = self.start_urls[0]
temp = '_octo=GH1.1.1045146750.1615451260; _device_id=cd8d64981fcb3fd4ba7f587873e97804'
# 把cookies字符串轉(zhuǎn)成字典
cookies = {data.split('=')[0]: data.split('=')[-1] for data in temp.split('; ')}
yield scrapy.Request(
url=url,
callback=self.parse,
cookies=cookies
)
def parse(self, response):
print(response.xpath('/html/head/title/text()').extract_first())以上就是python重寫start_requests方法,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
本文題目:創(chuàng)新互聯(lián)Python教程:python如何重寫start_requests方法
標題URL:http://www.5511xx.com/article/djdseoj.html


咨詢
建站咨詢
