Good people, tell me, please) I am a beginner, I ask you not to throw stones at me)) I want to parse the site, I ran into two problems at the same time, I am following the example of YouTube

but I get an error from encoding and I can’t understand whether the prettify () method works or not, but it seems not. With this code, this error

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import urllib.request from bs4 import BeautifulSoup import csv def get_html(url): response = urllib.request.urlopen(url) return response.read() def parse(html): soup = BeautifulSoup(html, 'html.parser') div = soup.find('div', class_='col-md-8 col-left') print(div.prettify()) def main(): parse(get_html('сайт')) if __name__ == '__main__': main() 

Error code

  #!/usr/bin/env python3 # -*- coding: utf-8 -*- import urllib.request from bs4 import BeautifulSoup import csv def get_html(url): response = urllib.request.urlopen(url) return response.read() def parse(html): soup = BeautifulSoup(html, 'html.parser') div = soup.find('div', class_='col-md-8 col-left', encoding='utf-8') print(div.prettify()) def main(): parse(get_html('тот же')) if __name__ == '__main__': main() 

then it gives an error like this: Pretiffy error

Actually the question, adding encoding on line 17, did I correct the encoding error? And if so, then the second question is why the prettify () method does not work? I did print (parse (get_html ('link to the site'))) in maine without this method and in response the program gave out None The question is where did I get it?)) Sorry that the question is probably stupid, but still)) Thanks!)

  • The div.prettify object does not have a prettify attribute. look what contains div - codename0082016
  • Thanks for the answer, the first code works on another computer, everything is clearly obtained, this is a problem with the encoding in my system or in the studio ... you do not know the encoding correctly and where? - Vladislav Kutsobin
  • And I understand that my code for some reason uses the cp866 encoding, although I should use yutf-8 - Vladislav Kutsobin
  • put a stopping point and compare the data before exiting - codename0082016
  • I do not understand (where to put the point? - Vladislav Kutsobin

1 answer 1

The issue is resolved, more precisely, I went around it. Reinstalled the python, demolished the studio, put the pacharm. everything is working

It can be seen when installing the last time the python, the encodings have moved out. It is not accurate. But a banal language reset and IDE change helped.