scrapy实现post请求与请求传参

不推荐使用scrapy框架发送post请求,配置复杂,如果在数据量大 的情况下,可以通过如下代码来实现:

复制代码
import scrapy


class 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链接写在外部,然后手动去发送请求  scrapy.FormRequest(url=url,formdata=data,callback=self.parse)

请求传参的实现:

复制代码
# -*- coding: utf-8 -*-
import scrapy
from video.items import VideoItem

class 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)            #电影类型 多标签   列表转字符串
        actor = response.xpath('//div[@class="ct-c"]/dl/dt[3]/text()').extract_first()
        about=response.xpath('//div[@class="ee"]/text()').extract_first()

        item['year']=year
        item['country'] =country
        item['type'] =type
        item['actor'] =actor
        item['about'] =about

        yield item

    def 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']=name
            yield scrapy.Request(url=m_url,callback=self.detail_parse,meta={'item':item})
复制代码

 item文件代码:

复制代码
import scrapy

class 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()
复制代码

 

posted @   青红*皂了个白  阅读(8975)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示