【问题标题】:convert lxml to scrapy xxs selector将 lxml 转换为 scrapy xxs 选择器
【发布时间】:2013-07-25 13:38:11
【问题描述】:

如何将这个纯 python lxml 转换为内置 xxs 选择器的 scrapy?这个可行,但我想将其转换为 scrapy xxs 选择器。

    def parse_device_list(self, response):
    self.log("\n\n\n List of devices \n\n\n")
    self.log('Hi, this is the parse_device_list page! %s' % response.url)
    root = lxml.etree.fromstring(response.body)
    for row in root.xpath('//row'):
        allcells = row.xpath('./cell')
        # first cell contain the link to follow
        detail_page_link = allcells[0].get("href")
        yield Request(urlparse.urljoin(response.url, detail_page_link ), callback=self.parse_page)

【问题讨论】:

    标签: python xml screen-scraping scrapy lxml


    【解决方案1】:

    试一试:

    def parse_page(self, response):
        xxs = XmlXPathSelector(response)
        for row in xxs.select('//row'):
            detail_page_link = row.select('.//cell[1]/@href')[0].extract()
            yield Request(urlparse.urljoin(response.url, detail_page_link), callback=self.parse_page)
    

    【讨论】:

    • 这似乎可行,但我怎样才能让它迭代,因为某种原因它会迭代列 A,但它是无序的。当 A 列中的行之一为空时,它会抓取 B 列链接。我可以让我的方法只获取 A 列,如果 A 列为空,则跳过它并转到 A 列的下一行。当 A 列为空时,不是 B 列。
    猜你喜欢
    • 2015-06-21
    • 2016-05-30
    • 1970-01-01
    • 2020-03-02
    • 2012-09-16
    • 2019-04-20
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多