Scrapy only returns first result

南笙酒味 提交于 2021-02-10 09:35:51

问题


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

Code seen here:

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

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

Any suggestions? Thanks!


回答1:


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")

Prints (there are multiple occurrences of $0.03):

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


来源:https://stackoverflow.com/questions/29481109/scrapy-only-returns-first-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!