Scrapy 只返回第一个结果 [英] Scrapy only returns first result

查看:55
本文介绍了Scrapy 只返回第一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取在这里看到的预格式化的 html.但我的代码只返回 1 个价格而不是所有 10 个价格.

I'm trying to scrape preformatted html seen here. But my code only returns 1 price instead of all 10 prices.

此处看到的代码:

class MySpider(BaseSpider):
    name = "working1"
    allowed_domains = ["steamcommunity.com"]
    start_urls = ["http://steamcommunity.com/market/search/render/?query=&appid=440"]

    def parse(self, response):
        sel = Selector(response)
        price = sel.xpath("//text()[contains(.,'$')]").extract()[0].replace('\\r\\n\\t\\t\\t\\r\\n\\t\\t\\t','')
        print price

我是scrapy/xpath的新手,所以我不太确定为什么它不打印价格的每个实例.

I'm super new to scrapy/xpath so I'm not really sure why it isn't printing every instance of the price.

有什么建议吗?谢谢!

推荐答案

您将获得 xpath 匹配的第一个结果.相反,迭代所有这些:

You are getting the first result of the xpath match. Instead, iterate over all of them:

for price in sel.xpath("//text()[contains(., '$')]").extract():
    print price.strip(r"\r\n\t")

打印(多次出现 $0.03):

$0.03
$0.03
$0.03
$0.03
$0.03
$0.03
$0.03
$0.03
$0.03
$0.03

这篇关于Scrapy 只返回第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆