import requests
from bs4 import BeautifulSoup
class Book:
def __init__(self, source):
self.source = source
self.headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0",
}
def get_response(self):
"""Get response"""
r = requests.get(self.source, headers=self.headers)
if r.status_code == 200:
return r.content
else:
return r.status_code
def __str__(self):
"""Get html content"""
return BeautifulSoup(self.get_response(), "lxml")
if "__main__" == __name__:
b = Book("https://www.example.com")
print(b.title) # AttributeError: 'Book' object has no attribute 'title'
Как бы мне получать ссылку на класс, __str__- возращается строка, а не ссылка на Объект.