【问题标题】:How to parse this info into individual items?如何将此信息解析为单个项目?
【发布时间】:2021-01-03 02:26:13
【问题描述】:

我使用以下 scrapy spider 从网页中抓取了以下信息。如何将此信息转换为单个项目,即一个项目应由名称、大小、链接、扩展名、月份和年份组成。

这是蜘蛛的代码:

import scrapy
from scrapy.crawler import CrawlerProcess


class MapSpider(scrapy.Spider):
    name = 'map'
    allowed_domains = ['map.gob.do']

    def start_requests(self):
        start_urls = [
            'https://map.gob.do/transparencia/recursos-humanos/nominas-de-empleados/']
        for url in start_urls:
            yield scrapy.Request(url=url, callback=self.parse,)

    def parse(self, response):
        panes = response.xpath('/html/body/div[8]/div/section/div/div/div[2]/div/div/div[3]/ul/li')
        tables = response.xpath('/html/body/div[8]/div/section/div/div/div[2]/div/div/div[3]/div/div')
        for pane in panes:
            Id = pane.css('::attr(href)').get(default='')
            Year = pane.css('::text').get(default='')
            yield{
                'year': Year,
                'id': Id
            }
        for d,table in enumerate(tables,1):
            yearId = table.css('.tab-pane ::attr(id)').get(default='')
            months = table.css('#'+ yearId + '.tab-pane .vr-tabs-nav-link ::text').getall()
            monthsIds = table.css('#'+ yearId + '.tab-pane .vr-tabs-nav-link ::attr(href)').getall()
            print(f'|||YEAR \' {d} \' INFO |||')
            yield{
                'yearId': yearId,
                'months': months,
                'monthsIds': monthsIds,
            }
            for c,monthId in enumerate(monthsIds,1):
                itemNames = table.css(monthId  + ' tr .wpfd_downloadlink ::attr(title)').getall()
                itemsLinks = table.css(monthId + ' tr.file .wpfd_downloadlink ::attr(href)').getall()
                itemsSizes = table.css(monthId + ' tr.file .file_size::text').getall()
                itemsExt = table.css(monthId + ' tr.file .wpfd_downloadlink > span > span ::attr(class)').getall()
                print(f'|||MONTH \' {c} \' INFO |||')
                yield {
                    'monthId': monthId,
                    'itemsNames': itemNames,
                    'itemsSizes': itemsSizes, 
                    'itemsLinks': itemsLinks,
                    'itemsExt': itemsExt
                }

process = CrawlerProcess()
process.crawl(MapSpider)
process.start()

【问题讨论】:

  • 您可能需要另一个 for-loop 和 zip() 来对单个项目的值进行分组
  • 或者您应该首先获取所有tr 并使用for-loop 分别处理每个tr。然后你应该得到个别行的值。它也可以具有优势 - 如果其中一行缺少数据,那么您的版本不会捕获它,但它会从下一行移动值而不是缺失值 - 您会得到错误的结果。分别使用每个tr,您可以捕获行中缺少的元素并输入一些默认值。
  • 感谢您的回答。我使用 Lukas 方法子索引来做到这一点,因为选择器为我提供了我正在寻找的信息,但我接受了你的提示,用默认值替换缺失值。现在我在将我的项目导入蜘蛛时遇到问题。它给了我一个 ModuleNotFoundError 我应该在这里问这个还是应该打开另一个问题。
  • 最好在新页面上创建新问题,因为您必须显示完整的错误消息。或者最好先在 Google 中检查这个错误——也许有人已经解决了这个问题——importat 是它找不到的模块。可能您的文件在错误的文件夹中。 Scrapy 期望项目中特殊文件夹中的蜘蛛和模型。
  • 我在 google 中环顾四周,发现有类似错误的人,但发布的解决方案都没有对我有用。在我提出一个新问题之前,我会再试一试。

标签: python python-3.x web-scraping scrapy


【解决方案1】:

目前您的table.css(...).getall() 返回多个值,您将它们全部打包到yield 中。与 return 相比,yield 的优势在于您还可以选择块大小。

将一般收益替换为您想要的更具体的收益。例如

for i in range(min(map(len, [itemNames, itemsLinks, itemsSizes, itemsExt]))):
    yield {
         'monthId': monthId,
         'itemsNames': itemNames[i],
         'itemsSizes': itemsSizes[i], 
         'itemsLinks': itemsLinks[i],
         'itemsExt': itemsExt[i]
          }

【讨论】:

  • 感谢您的回答,非常有帮助。现在我在将我的项目导入蜘蛛时遇到问题。它给了我一个 ModuleNotFoundError。我应该为此提出一个新问题吗?
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2014-12-09
相关资源
最近更新 更多