html decode

error:

 /usr/bin/python3 /home/cor/webscrappython/Web_Scraping_with_Python/chapter01/link_crawler2.py
Downloading:http://example.webscraping.com
Downloading --- 5
Traceback (most recent call last):
  File "/home/cor/webscrappython/Web_Scraping_with_Python/chapter01/link_crawler2.py", line 38, in <module>
    link_crawler('http://example.webscraping.com', '/(index|view)')
  File "/home/cor/webscrappython/Web_Scraping_with_Python/chapter01/link_crawler2.py", line 16, in link_crawler
    for link in get_links(html):
  File "/home/cor/webscrappython/Web_Scraping_with_Python/chapter01/link_crawler2.py", line 34, in get_links
    return webpage_regex.findall(html)
TypeError: cannot use a string pattern on a bytes-like object

  solution:

you want to convert html (a byte-like object) into a string using .decode,
e.g. html = response.read().decode('utf-8')

  

原文地址:https://www.cnblogs.com/winditsway/p/12567416.html