scrapy 为什么会出现此括号未闭合错误

lfapxunr  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(100)

我试图学习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")

error

wvt8vs2t

wvt8vs2t1#

最后一行,您将引号(“)嵌套在用引号声明的字符串中。
使用\”转义引号或使用'而不是“声明引号

7tofc5zh

7tofc5zh2#

因为您没有使用双引号的转义键
我更正如下:

def parse_country(self, response):
        rows = response.xpath("(//table[@class=\"table table-striped table-bordered table-hover table-condensed table-list\"])[1]/tbody/tr")

相关问题