我想刮掉这个页面的表,但是废弃的数据只在一列中,而且在某些情况下数据不会出现。另外,我使用shell查看Xpath是否正确(我使用Xpath助手来标识这些Xpath)
import scrapy
class ToScrapeSpiderXPath(scrapy.Spider):
name = 'scrape-xpath'
start_urls = [
'http://explorer.eu/contents/food/28?utf8=/',
]
def parse(self, response):
for flv in response.xpath('//html/body/main/div[4]'):
yield {
'Titulo': flv.xpath('//*@id="chromatography"]/table/tbody/tr[3]/th/strong/a/text()"]/tbody/tr[5]/td[3]/a[2]').extract(),
'contenido': flv.xpath('//*@id="chromatography"]/table/tbody/tr[5]/td[3]/a[2]/text()').extract(),
'clase': flv.xpath('//*[@id="chromatography"]/table/tbody/tr[5]/td[1]/text()').extract(),
'Subclase': flv.xpath('//*[@id="chromatography"]/table/tbody/tr[5]/td[2]/a/text').extract(),
}
从给定的示例URL来看,对于包含更多记录的页面,其值应该是什么以及提取应该如何进行泛化并不十分明显。所以我尝试了一个不同的页面,包含多个记录,让我们看看结果是否符合您的需要。下面是准备运行的代码:
基本上,它迭代表的各个行,并从相应的字段中提取数据,一旦收集到完整的信息,就生成一个项。在
试试这个:
请记住,内部循环选择器也应该是相对于节点
flv
的,使用//
进行选择是一个全局选择器,因此它将获取所有内容。 最好检查真实的html代码,因为浏览器可能会呈现与实际接收到的html不同的其他代码(例如tbody
标记)相关问题 更多 >
编程相关推荐