Trying to write a parser (for the first time in my life). Here I try to do a search on the "Championship" It gives an error

'NoneType' object has no attribute 'prettify'

It seems that something is wrong

import urllib.request from bs4 import BeautifulSoup def get_html(url): response = urllib.request.urlopen(url) return response.read() def parse(html): soup = BeautifulSoup(html) table = soup.find('div',class_='livetable zbyqj js-livetable') print(table.prettify()) print(parse(get_html('https://www.championat.com/'))) 
  • in BeautifulSoup(html) add the argument lxml BeautifulSoup(html, "lxml") - Twiss
  • Did not help. Maybe I'm looking for the wrong? - Dimabytes
  • Obviously, your None (find () table didn't find anything). Take the minimal piece of html that the search div contains, open the Python REPL and interactively try searching using different arguments. If it does not work out, add this html to the question, remove urlopen () (replace return html_str with return html_str ) and explicitly state what specific element you want to find ¶ It’s better to use the select() method to avoid thinking about the number of classes. crummy.com/software/BeautifulSoup/bs4/doc/… - jfs
  • And what specific information is needed? - Alexander

1 answer 1

 import requests from bs4 import BeautifulSoup headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2)\ AppleWebKit/537.36 (KHTML, like Gecko)\ Chrome/63.0.3239.84 Safari/537.36', 'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'} url = "https://www.championat.com/" def parser(): try: page = requests.get(url, headers=headers).content item = BeautifulSoup(page, 'lxml') data = item.find('div', {'class': 'livetable-bar tabs js-livetable-bar'}).prettify() text = item.find('a', {'href': '/stat/'}).text print(data) print(text) except requests.exceptions.ConnectionError: print("Нет соединения с интернетом...") if __name__ == '__main__': parser()