Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Python: How to call class method from imported module? "Self" argument issue

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.

I got this error:

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 ('	', 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()

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
from cinemas import cinemas_info

You are importing the class definition here. Now the next line:

cinemas_info.prepare(url)

you are calling prepare on this class definition. What you want is to instantiate the class first, and call prepare() on the instance of the class.

ci = cinemas_info(url)
ci.prepare(url)  # this works

EDIT: When you call a method on a instance, the self variable is automatically populated by Python as a reference to the instance of the object, so you only need to pass in one argument to def prepare(self, url).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...