【问题标题】:Scrapy: Follow link to get additional Item data?Scrapy:点击链接以获取其他项目数据?
【发布时间】:2012-03-09 05:21:10
【问题描述】:

我没有具体的代码问题我只是不确定如何使用 Scrapy 框架从逻辑上解决以下问题:

我要抓取的数据结构通常是每个项目的表格行。够直白了吧?

最终我想为每一行抓取 TitleDue DateDetailsTitleDue Date 在页面上立即可用...

但是详细信息本身不在表格中——而是指向包含详细信息的页面的链接(如果没有意义,这里是表格):

|-------------------------------------------------|
|             Title              |    Due Date    |
|-------------------------------------------------|
| Job Title (Clickable Link)     |    1/1/2012    |
| Other Job (Link)               |    3/2/2012    |
|--------------------------------|----------------|

恐怕我仍然不知道如何通过回调和请求从逻辑上传递项目,即使在阅读了 Scrapy 文档的 CrawlSpider 部分之后。

【问题讨论】:

标签: hyperlink callback scrapy


【解决方案1】:

请先阅读docs 以了解我的意思。

答案:

要抓取其他页面上的其他字段,在 parse 方法中提取带有附加信息的页面的 URL,创建并从该 parse 方法返回具有该 URL 的 Request 对象,并通过其 meta 参数传递已提取的数据.

how do i merge results from target page to current page in scrapy?

【讨论】:

【解决方案2】:

来自scrapy documentation的例子:

def parse_page1(self, response):
    item = MyItem()
    item['main_url'] = response.url
    request = scrapy.Request("http://www.example.com/some_page.html",
                             callback=self.parse_page2)
    request.meta['item'] = item
    yield request

def parse_page2(self, response):
    item = response.meta['item']
    item['other_url'] = response.url
    yield item

【讨论】:

    【解决方案3】:

    您还可以使用 Python functools.partial 通过附加参数将 item 或任何其他可序列化数据传递给下一个 Scrapy 回调。

    类似:

    import functools
    
    # Inside your Spider class:
    
    def parse(self, response):
      # ...
      # Process the first response here, populate item and next_url.
      # ...
      callback = functools.partial(self.parse_next, item, someotherarg)
      return Request(next_url, callback=callback)
    
    def parse_next(self, item, someotherarg, response):
      # ...
      # Process the second response here.
      # ...
      return item
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多