Python Scrapy刮取错误信息

2024-09-30 04:26:57 发布

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

我试图搜集一些数据,但输出结果很奇怪。我从这一页抓取的信息是国家名称和人口。但结果是:

['United States'] 328,131,075

这对我来说毫无意义,因为我正在从包含德国信息的页面上抓取

这是我的代码:

allowed_domains = ["geoba.se/country.php?cc=DE"]
start_urls = ['http://geoba.se/country.php?cc=DE/']

def parse(self, response):

    country = response.xpath('//*[@id="wrap"]/div[2]/div[2]/div[1]/div[1]/h2/b/text()').extract()
    pop_total = response.xpath('//td/div/table/tr/td[2]/text()').extract_first()

    print(country, pop_total)

我错过了什么?结果应该是:

['Germany'] 80,594,017

当我使用scrapy shell时,我得到了正确的输出


Tags: textdiv信息responseextractdepopcountry

热门问题