使用scrapy从HTML表中提取数据:响应.xpath()产生非

2024-05-11 22:44:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用scrapy库在python3中构建了一个web scraper,遇到了一个我不理解的问题。我已经成功地在表上使用inspect元素来获取xpath变量。但是,对于这个表,我无法确定如何从表中提取数据。我是HTML新手,但不是编程新手,所以如果我走远了请帮帮我。在

这个网页的一个例子是:http://land.elpasoco.com/ResidentialBuilding.aspx?schd=5317443025&bldg=1

检查页面并获取目标表的xpath得到//*[@id="aspnetForm"]/table/tbody/tr[3]/td[1]/table/tbody/tr[1]/td/table/tbody/tr[3]/td/table

但是,在一个蹩脚的shell response.xpath(target).extract()中使用它将返回[]。尝试以任何单个单元格为目标也会产生相同的空结果。我的预期结果将是一个数据帧或字典,将类似{'Dwelling Units': 1, 'Year Built': 2010 ... }相关联的任何帮助都可以识别出我出错的地方,或者如何将数据格式化。谢谢!在


Tags: 数据web元素目标htmltablescraperxpath
1条回答
网友
1楼 · 发布于 2024-05-11 22:44:54
import scrapy


class ResidentialRecordsSpider(scrapy.Spider):
    name = "residential_records"

    start_urls = [
        'http://land.elpasoco.com/ResidentialBuilding.aspx?schd=5317443025&bldg=1',
    ]

    def parse(self, response):
        for record in response.xpath('//table[@width="90%"]//td'):
            key = record.xpath('./strong/text()').extract_first(default='')
            value = record.xpath('./text()').extract_first(default='')

            yield { key: value }

这里只需要执行一些数据清理

相关问题 更多 >