gpt4 book ai didi

python - 为什么我的 Scrapy 代码返回一个空数组?

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:47 24 4
gpt4 key购买 nike

我正在为 wunderground.com 构建网络抓取工具,但我的代码为 inches_rain 和 humidity 返回了“[]”的值。谁能看出为什么会这样?

# -*- coding: utf-8 -*-
import scrapy
from scrapy.selector import Selector
import time

from wunderground_scraper.items import WundergroundScraperItem


class WundergroundComSpider(scrapy.Spider):
name = "wunderground"
allowed_domains = ["www.wunderground.com"]
start_urls = (
'http://www.wunderground.com/q/zmw:10001.5.99999',
)

def parse(self, response):
info_set = Selector(response).xpath('//div[@id="current"]')
list = []
for i in info_set:
item = WundergroundScraperItem()
item['description'] = i.xpath('div/div/div/div/span/text()').extract()
item['description'] = item['description'][0]
item['humidity'] = i.xpath('div/table/tbody/tr/td/span/span/text()').extract()
item['inches_rain'] = i.xpath('div/table/tbody/tr/td/span/span/text()').extract()
list.append(item)
return list

我也知道 humidity 和 inches_rain 项目设置为相同的 xpath,但这应该是正确的,因为一旦信息在数组中,我就将它们设置为数组中的特定值。

最佳答案

为了举例,让我建议一个更可靠和可读的 XPath 来定位“湿度”值,其中基础是“湿度”列标签:

"".join(i.xpath('.//td[dfn="Humidity"]/following-sibling::td//text()').extract()).strip()

现在输出 45%。


仅供引用,您的 XPath 至少有一个问题 - tbody 标记 - 将其从 XPath 表达式中删除。

关于python - 为什么我的 Scrapy 代码返回一个空数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31191070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com