【发布时间】:2017-03-22 17:48:52
【问题描述】:
我正在尝试使用 Scrapy 和 xpath 从站点抓取数据,但遇到了一些麻烦。这是我的代码:
class MaijiaSpider(scrapy.Spider):
name = 'maijiaSpider'
start_urls =["http://www.maijia.com/index.html#/item/list/?keyword=recaro"]
def parse(self, response):
articles = response.xpath("//table[@class='ui-table ui-table-striped ui-table-inbox tablefixed']//tr[1]/td[2]/div/div[1]/a/@href")
for article in articles:
yield{
'link': article.xpath('.//td[2]//a/@href').extract_first()
}
问题是文章总是空的,因此它永远不会进入 for 循环。我究竟做错了什么?我尝试了不同的 xpath 字符串,但似乎没有任何效果。
【问题讨论】:
-
这是一个相当长的 XPath 表达式。 '//div[@class="cell-box"]/a' 怎么样?出错的机会更少。
-
如何在
start-urls中从URL获取页面源代码? -
@leovp 我也试过了,但还是不行
-
@Andersson 对不起,我不明白这个问题!我刚开始使用scrapy,所以我的知识真的很有限