我试图搜集一些数据,但输出结果很奇怪。我从这一页抓取的信息是国家名称和人口。但结果是:
['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时,我得到了正确的输出
问题在于您的起始URL。它应该是“http://geoba.se/country.php?cc=DE”(没有最后的斜杠),否则网站会一直把你带到美国页面
相关问题 更多 >
编程相关推荐