【问题标题】:Not Sure What to Iterate With Scrapy不确定用 Scrapy 迭代什么
【发布时间】:2012-04-11 05:46:12
【问题描述】:

我在使用 scrapy 迭代爬网时遇到问题。我正在提取一个标题字段和一个内容字段。问题是我得到了一个 JSON 文件,其中列出了所有标题,然后是所有内容。我想获得 {title}、{content}、{title}、{content},这意味着我可能必须遍历 parse 函数。问题是我无法弄清楚我正在循环的元素是什么(即for x in [???])这是代码:

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.spiders import SitemapSpider

from Foo.items import FooItem


class FooSpider(SitemapSpider):
    name = "foo"
    sitemap_urls = ['http://www.foo.com/sitemap.xml']
    #sitemap_rules = [


    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        items = [
        item = FooItem()
        item['title'] = hxs.select('//span[@class="headline"]/text()').extract()
        item['content'] = hxs.select('//div[@class="articletext"]/text()').extract()
        items.append(item)
        return items

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    您的 xpath 查询返回页面上的所有标题和所有内容。我想你可以这样做:

    titles = hxs.select('//span[@class="headline"]/text()').extract()
    contents = hxs.select('//div[@class="articletext"]/text()').extract()
    
    for title, context in zip(titles, contents):
        item = FooItem()
        item['title'] = title
        item['content'] = context
        yield item
    

    但这并不可靠。尝试执行返回带有titlecontent 的块的xpath 查询。如果你给我看 xml 源,我会帮你的。

    blocks = hxs.select('//div[@class="some_filter"]')
    for block in blocks:
        item = FooItem()
        item['title'] = block.select('span[@class="headline"]/text()').extract()
        item['content'] = block.select('div[@class="articletext"]/text()').extract()
        yield item
    

    我不确定 xpath 查询,但我认为想法很明确。

    【讨论】:

      【解决方案2】:

      您不需要HtmlXPathSelector。 Scrapy 已经内置了 XPATH 选择器。试试这个:

      blocks = response.xpath('//div[@class="some_filter"]')
      for block in blocks:
          item = FooItem()
          item['title'] = block.xpath('span[@class="headline"]/text()').extract()[0]
          item['content'] = block.xpath('div[@class="articletext"]/text()').extract()[0]
          yield item
      

      【讨论】:

        猜你喜欢
        • 2023-01-20
        • 1970-01-01
        • 2011-02-05
        • 2023-04-07
        • 2021-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-09
        相关资源
        最近更新 更多