问题
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