def fetch_html(url):
# ungood idea to assume its UTF-8. Try to read header
try:
fp = urllib.request.urlopen(url)
fpbytes = fp.read()
html = fpbytes.decode("utf8")
fp.close()
print("Success! {} chars found".format(len(html)))
return html
except:
print("Failed to extract html, retrying again in a few seconds")
time.sleep(3.5)
fetch_html(url)
url = "https://i.reddit.com/r/AskReddit/top/.compact?sort=top&t=day"
html = fetch_html(url)
print(html)
html仍然没有,尽管它给出了len(html)70000,这有什么用呢?我尝试切换顺序,在返回html之后放置fp.close(),但仍然会出现相同的错误。
我已经在google中搜索了此内容,尽管他们的问题来自于不使用其值的收益,这与这个问题有所不同。