I'm trying to save the contents of a URL to a text file. I found several some sample scripts online to do this, and the two below seem like good candidates to help me do what I want to do, but both return this error:
TypeError: a bytes-like object is required, not 'str'
import html2text
import urllib.request
with urllib.request.urlopen("http://www.msnbc.com") as r:
html_content = r.read()
rendered_content = html2text.html2text(html_content)
file = open('C:\\Users\\Excel\\Desktop\\URL.txt', 'w')
file.write(rendered_content)
file.close()
import sys
if sys.version_info[0] == 3:
from urllib.request import urlopen
else:
# Not Python 3 - today, it is most likely to be Python 2
# But note that this might need an update when Python 4
# might be around one day
from urllib import urlopen
# Your code where you can use urlopen
with urlopen("http://www.msnbc.com") as r:
s = r.read()
rendered_content = html2text.html2text(html_content)
file = open('C:\\Users\\Excel\\Desktop\\URL.txt', 'w')
file.write(rendered_content)
file.close()
I'm probably missing something simple here, but I can't tell what it is.
I am using Python 3.6.