Python:如何从导入的模块调用类方法? "自"论证问题 [英] Python: How to call class method from imported module? "Self" argument issue

查看:344
本文介绍了Python:如何从导入的模块调用类方法? "自"论证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件。我知道如何调用函数,但无法理解如何正确地从导入模块调用一个方法,该模块将self作为第一个参数。

I have 2 files. I know how to call a function, but cant understand how properly to call a method from imported module which has "self" as the 1st argument.

我收到此错误:


TypeError:prepare()缺少1个必需的位置参数:'url'

TypeError: prepare() missing 1 required positional argument: 'url'



cinemas.py    

import requests
from lxml.html import fromstring

class cinemas_info():

    def __init__(self, url):
        self.basic_cinemas_info(url)

    def prepare(self, url):


        url = requests.get(url)
        tree = fromstring(url.text)
        tree.make_links_absolute(url.url)
        return tree

    def basic_cinemas_info(self, url):


        tree = self.prepare(url)

        for city in tree.xpath(".//div[@class='city-caption']"):
            city1 = city.xpath("text()")[0]
            for cinema in city.xpath("following-sibling::*[1]/li/a"):
                name1 = cinema.xpath("text()")[0]
                detailed_url = cinema.xpath("@href")[0]
                print (city1.strip(), name1.strip(), ':')
                self.detailed_cinemas_info(detailed_url)
                self.detailed_cinemas_films(detailed_url)

    def detailed_cinemas_info(self, url):


        tree = self.prepare(url)

        for street in tree.xpath(".//div[@class='address']"):
            street1 = street.xpath("text()")[0]
            for phone in tree.xpath(".//div[@class='phone']"):
                phone1 = phone.xpath("text()")[0]
                for website in tree.xpath(".//div[@class='website']/a"):
                    website1 = website.xpath("@href")[0]
                    print ('\t', street1.strip(), phone1.strip(), website1.strip())

    def detailed_cinemas_films(self, url):


        showtimes_tab_url = '/showtimes/#!=&cinema-section=%2Fshowtimes%2F'
        tree = self.prepare(url + showtimes_tab_url)

        for film in tree.xpath('//div[@class="content"]'):
            film_name = film.xpath('.//a[@class="navi"]/text()')[0]
            for dates in film.xpath('.//li[contains(@class,"showtimes-day sdt")]'):
                film_dates = dates.xpath('.//div[@class="date"]/text()')[0]
                for times in dates.xpath('.//ul[@class="showtimes-day-block"]/li'):
                    film_times = times.xpath('a/text()')

                    if len(film_times) == 0:
                        film_times = None

                    is_3d = times.find('span')
                    if film_times is not None:
                        if is_3d is not None:
                            print(film_dates, film_name, film_times[0], '3D')
                        else:
                            print(film_dates, film_name, film_times[0])

if __name__ == "__main__":
    cinemas_info('http://vkino.com.ua/cinema/#!=')

#films.py

import requests
from cinemas import cinemas_info

def films_list():
    url = 'http://vkino.com.ua/afisha#!='
    tree = cinemas_info.prepare(url) #  <<<----Here is my call

    for films in tree.xpath('//*[@id="content"]/div/div[1]/ul[2]/li'):
        film_name = films.xpath('.//div[@class="title"]/a/text()')[0]
        film_url = films.xpath('.//div[@class="title"]/a/@href')[0]
        print(film_name, film_url)

if __name__ == "__main__":
    films_list()

我知道我可以将def prepare移出课堂并删除自我论点,但我想知道,什么是正确的方法至在类中调用def prepare。

I know that I can move "def prepare" out of class and remove "self" argument, but I want to know, what is the right way to call "def prepare" inside of a class.

推荐答案

由于的定义准备中有 self ,您可以在 cinemas_info 的实例上调用此方法,也可以通过传递实例它和 url prepare()

Since the definition of prepare has self in it, you can call this method either on an instance of cinemas_info or by passing an instance of it and url to prepare().

所以你只需要创建一个 cinemas_info 类的实例,Python本身就会将该实例传递给 prepare()。您只需要传递 url

So you just need to create an instance of cinemas_info class and Python will itself pass that instance to prepare(). You just need to pass the url.

#films.py

import requests
from cinemas import cinemas_info

def films_list():
    url = 'http://vkino.com.ua/afisha#!='
    c_info = cinemas_info(url) # created an instance of cinemas_info
    tree = c_info.prepare(url) # can now call prepare() on this instance

    for films in tree.xpath('//*[@id="content"]/div/div[1]/ul[2]/li'):
        film_name = films.xpath('.//div[@class="title"]/a/text()')[0]
        film_url = films.xpath('.//div[@class="title"]/a/@href')[0]
        print(film_name, film_url)

if __name__ == "__main__":
    films_list()

这篇关于Python:如何从导入的模块调用类方法? &QUOT;自&QUOT;论证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆