用羊皮和樱桃糖?

2024-06-28 15:02:16 发布

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

我有两个班,一个是乡村班,另一个是乡村班。我想在countries类中从CountriesSpider调用parse()函数,但出现了以下错误

TypeError: parse() missing 1 required positional argument: 'response'

请注意:def parse(self,response)是类scrapy.Spider的一个内置函数,它接受reponse参数

如何从countries类调用this函数self.countriesSpider.parse()

import scrapy
import cherrypy

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):        
        title = response.xpath("//h1/text()").get()        
        countries = response.xpath("//td/a/text()").getall()

        yield {
            'title': title,
            'countries': countries
        }

        return countries


class Countries(object):

    def __init__(self):
        # Create an instance of Snacks class
        self.countriesSpider = CountriesSpider()

    @cherrypy.expose    
    def index(self):
        return self.countriesSpider.parse()

if __name__ == '__main__':
    cherrypy.quickstart(Countries())

Tags: 函数importselftitleparseresponsedefcountries