python - 如何删除scrapy函数中的项目名称?

标签 python scrapy

运行编码时,它给出了商品价格,但商品名称相同。意味着它给出 transcription_price: 245,然后给出 transcription_price: 240。应该有 caption_pricetranscription_price。为什么以及如何解决这个问题?

import scrapy
from .. items import FetchingItem
import re

class SiteFetching(scrapy.Spider):
name = 'Site'
start_urls = ['https://www.rev.com/freelancers/transcription',
          'https://www.rev.com/freelancers/captions']

def parse(self, response):
    items = FetchingItem()
    Transcription_price = response.css('#middle-benefit .mt1::text').extract()

    items['Transcription_price'] = Transcription_price

    def next_parse(self, response):
        other_items = FetchingItem()
        Caption_price = response.css('#middle-benefit .mt1::text').extract()

        other_items['Caption_price'] = Caption_price
        yield other_items

    yield items

最佳答案

您的代码永远不会到达方法self.next_parse。默认情况下,Scrapy 对 self.start_urls 中的每个 URL 调用回调 self.parse。 您可以通过重写方法 start_requests 来使用自定义回调。

具体操作方法如下:

import scrapy
from .. items import FetchingItem
import re

class SiteFetching(scrapy.Spider):
    name = 'Site'

    def start_requests(self):
        return [
            scrapy.Request('https://www.rev.com/freelancers/transcription', callback=self.parse_transcription),
            scrapy.Request('https://www.rev.com/freelancers/captions', callback=self.parse_caption)
        ]

    def parse_transcription(self, response):
        items = FetchingItem()
        Transcription_price = response.css('#middle-benefit .mt1::text').extract()

        items['Transcription_price'] = Transcription_price
        yield items

    def parse_caption(self, response):
        other_items = FetchingItem()
        Caption_price = response.css('#middle-benefit .mt1::text').extract()

        other_items['Caption_price'] = Caption_price
        yield other_items

请参阅Spider documentation了解更多信息。

关于python - 如何删除scrapy函数中的项目名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044280/

相关文章:

python - 如何使用 Python 获得直方图中可变 bin 范围的相同 bin 宽度?

python - 在 Scrapy Start URL 中输入序列的第 N 个数字

python - Scrapy - 抓取每个项目的多个页面

python - 如何在 django View 中使用参数运行 scrapy 蜘蛛

python - 来自 Pandas 混淆矩阵的 Bokeh 热图

python - 按两个分类变量嵌套分组的 Altair 箱线图

python - 多页 scrapy 代码的最佳实践

javascript - Scrapy如何与Javascript打交道

python - 如何创建一个执行插入但该函数必须处理具有不同列数的表的函数?

python - 比较字符串,同时忽略行结尾