Python: How to call class method from imported module? "Self" argument issue -
i have 2 files. know how call function, cant understand how call method imported module has "self" 1st argument.
i got error:
typeerror: prepare() missing 1 required positional argument: 'url'
cinemas.py import requests 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) city in tree.xpath(".//div[@class='city-caption']"): city1 = city.xpath("text()")[0] 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) street in tree.xpath(".//div[@class='address']"): street1 = street.xpath("text()")[0] phone in tree.xpath(".//div[@class='phone']"): phone1 = phone.xpath("text()")[0] 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) film in tree.xpath('//div[@class="content"]'): film_name = film.xpath('.//a[@class="navi"]/text()')[0] dates in film.xpath('.//li[contains(@class,"showtimes-day sdt")]'): film_dates = dates.xpath('.//div[@class="date"]/text()')[0] 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 not none: if is_3d 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 cinemas import cinemas_info def films_list(): url = 'http://vkino.com.ua/afisha#!=' tree = cinemas_info.prepare(url) # <<<----here call 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 can move "def prepare" out of class , remove "self" argument, want know, right way call "def prepare" inside of class.
since definition of prepare
has self
in it, can call method either on instance of cinemas_info
or passing instance of , url
prepare()
.
so need create instance of cinemas_info
class , python pass instance prepare()
. need pass url
.
#films.py import requests cinemas import cinemas_info def films_list(): url = 'http://vkino.com.ua/afisha#!=' c_info = cinemas_info(url) # created instance of cinemas_info tree = c_info.prepare(url) # can call prepare() on instance 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()
Comments
Post a Comment