我将此代码片段作为 python 代码的一部分来抓取特定网站(请参阅下面的代码)。但令我惊讶的是,输出代码不是 html。我正在使用 python 3.4
import urllib.request as ur
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
s = ur.urlopen('http://www.nairaland.com')
pl = s.read()
print(pl)
这段代码的输出是:
b''
而不是预期的 html 代码。请指导我让这段代码正常工作。我需要代码的另一部分的html代码。提前致谢。
最佳答案
优秀的requests
库返回正确的HTML:
import requests
s = requests.get('http://www.nairaland.com')
pl = s.text
print(pl)
关于python - 我的python代码输出错误的html数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736055/