【问题标题】:Scrapy unable to scrape items, xpath not workingScrapy 无法抓取项目,xpath 无法正常工作
【发布时间】:2017-07-17 23:15:59
【问题描述】:

我花了很多时间尝试用 scrapy 抓取信息但没有成功。 我的目标是浏览类别并为每个项目抓取标题、价格和标题的 href 链接。

问题似乎来自 parse_items 函数。我已经用 firepath 检查了 xpath,我可以根据需要选择项目,所以也许我只是不知道 scrapy 如何处理 xpath...

这是我的代码

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.selector import Selector
from ..items import electronic_Item


class robot_makerSpider(CrawlSpider):
    name = "robot_makerSpider"
    allowed_domains = ["robot-maker.com"]
    start_urls = [
        "http://www.robot-maker.com/shop/",
    ]

    rules = (

        Rule(LinkExtractor(
            allow=(
                "http://www.robot-maker.com/shop/12-kits-robots",
                "http://www.robot-maker.com/shop/36-kits-debutants-arduino",
                "http://www.robot-maker.com/shop/13-cartes-programmables",
                "http://www.robot-maker.com/shop/14-shields",
                "http://www.robot-maker.com/shop/15-capteurs",
                "http://www.robot-maker.com/shop/16-moteurs-et-actionneurs",
                "http://www.robot-maker.com/shop/17-drivers-d-actionneurs",
                "http://www.robot-maker.com/shop/18-composants",
                "http://www.robot-maker.com/shop/20-alimentation",
                "http://www.robot-maker.com/shop/21-impression-3d",
                "http://www.robot-maker.com/shop/27-outillage",
                ),
            ),
            callback='parse_items',
        ),
    )


    def parse_items(self, response):
        hxs = Selector(response)
        products = hxs.xpath("//div[@id='center_column']/ul/li")
        items = []

        for product in products:
            item = electronic_Item()
            item['title'] = product.xpath(
                "li[1]/div/div/div[2]/h2/a/text()").extract()
            item['price'] = product.xpath(
                "div/div/div[3]/div/div[1]/span[1]/text()").extract()
            item['url'] = product.xpath(
                "li[1]/div/div/div[2]/h2/a/@href").extract()
            
            #check that all field exist
            if item['title'] and item['price'] and item['url']:
                items.append(item)
        return items

感谢您的帮助

【问题讨论】:

    标签: xpath scrapy


    【解决方案1】:

    您的蜘蛛中的 xpath 确实有问题。

    您的第一个产品 xpath 确实有效,但不够明确,可能很容易失败。虽然产品详细信息 xpath 根本不起作用。

    我已经解决了:

    products = response.xpath("//div[@class='product-container']")
    items = []
    
    for product in products:
        item = dict()
        item['title'] = product.xpath('.//h2/a/text()').extract_first('').strip()
        item['url'] = product.xpath('.//h2/a/@href').extract_first()
        item['price'] = product.xpath(".//span[contains(@class,'product-price')]/text()").extract_first('').strip()
    

    所有现代网站都有对解析非常友好的 html 源代码(因为他们需要自己解析它以实现其花哨的 css 样式和 javascript 函数)。

    因此,通常您应该使用浏览器检查工具(右键单击 -> 检查元素)查看要提取的节点的类和 id 名称,而不是使用某些自动选择工具。它更可靠,一旦你掌握了它就不需要更多的工作。

    【讨论】:

    • 谢谢你!从这里开始我会小心的。你能解释一下直接从响应中查找 xpath 而不是使用 Selector(response) 方法有什么影响吗?
    • @ArtFilPortraitArtistetisseu 本质上是一回事。响应对象使用自己创建Selector,因此您可以方便地使用response.selector 的快捷方式,而不必每次都自己创建选择器。而response.xpathresponse.selector.xpath 的快捷方式。 source for Response 非常简单,您可以自己给它一个高峰:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多