scrapy实现post请求与请求传参
生活随笔
收集整理的這篇文章主要介紹了
scrapy实现post请求与请求传参
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
不推薦使用scrapy框架發(fā)送post請求,配置復雜,如果在數(shù)據(jù)量大 的情況下,可以通過如下代碼來實現(xiàn):
import scrapyclass FySpider(scrapy.Spider):name = 'fy'# allowed_domains = ['www.baidu.com']start_urls = ['https://fanyi.baidu.com/sug']def start_requests(self):data={'kw':"beautiful"}for url in self.start_urls:yield scrapy.FormRequest(url=url,formdata=data,callback=self.parse)def parse(self, response):print(response.text)方法一:就是重寫scrapy下面的start_requests方法
方法二:將URL鏈接寫在外部,然后手動去發(fā)送請求 scrapy.FormRequest(url=url,formdata=data,callback=self.parse)
請求傳參的實現(xiàn):
# -*- coding: utf-8 -*- import scrapy from video.items import VideoItemclass MvSpider(scrapy.Spider):name = 'mv'# allowed_domains = ['www.piaohua.com/']start_urls = ['http://www.88ys.cc/dianying/1.html']def detail_parse(self,response):item=response.meta['item']year=response.xpath('//div[@class="ct-c"]/dl/dd[3]/text()').extract_first()country = response.xpath('//div[@class="ct-c"]/dl/dd[2]/text()').extract_first()type_list=response.xpath('//div[@class="ct-c"]/dl/dt//a/text()').extract()type=" ".join(type_list) #電影類型 多標簽 列表轉(zhuǎn)字符串actor = response.xpath('//div[@class="ct-c"]/dl/dt[3]/text()').extract_first()about=response.xpath('//div[@class="ee"]/text()').extract_first()item['year']=yearitem['country'] =countryitem['type'] =typeitem['actor'] =actoritem['about'] =aboutyield itemdef parse(self, response):li_list=response.xpath('//div[@class="index-area clearfix"]/ul/li/a')item=VideoItem()for li in li_list:m_url='http://www.88ys.cc'+li.xpath('./@href').extract_first()name=li.xpath('./@title').extract_first()item['name']=nameyield scrapy.Request(url=m_url,callback=self.detail_parse,meta={'item':item})?item文件代碼:
import scrapyclass VideoItem(scrapy.Item):# define the fields for your item here like:name = scrapy.Field()year = scrapy.Field()country = scrapy.Field()type = scrapy.Field()actor = scrapy.Field()about = scrapy.Field()?
轉(zhuǎn)載于:https://www.cnblogs.com/wen-kang/p/10960169.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的scrapy实现post请求与请求传参的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可变参数的宏定义
- 下一篇: windows7环境下的http-ser