我试图学习scrapy,但我遇到了这个错误,似乎找不到它是从哪里来的。错误出现在最后一行下的(after response.xpath
import scrapy
import logging
class CountriesSpider(scrapy.Spider):
name = 'countries'
allowed_domains = ['www.worldometers.info']
start_urls = ['https://www.worldometers.info/world-population/population-by-country/']
def parse(self, response):
countries = response.xpath("//td/a")
for country in countries:
name = country.xpath(".//text()").get()
link = country.xpath(".//@href").get()
# absolute_url = f"htpps://www.worldometers.info{link}"
# absolute_url = response.urljoin(link)
# yield scrapy.Request(url=absolute_url)
yield response.follow(url=link, callback=self.parse_country)
def parse_country(self, response):
rows = response.xpath("(//table[@class="table table-striped table-bordered table-hover table-condensed table-list"])[1]/tbody/tr")
2条答案
按热度按时间wvt8vs2t1#
最后一行,您将引号(“)嵌套在用引号声明的字符串中。
使用\”转义引号或使用'而不是“声明引号
7tofc5zh2#
因为您没有使用双引号的转义键
我更正如下: