python - 我的python代码输出错误的html数据

标签 python html web-crawler

我将此代码片段作为 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/

相关文章:

python - 狮身人面像浮点格式

javascript - 删除继承的样式

javascript - 更改视频源而不停止 HTML5 视频

html - 在表格数据中添加 "sidebar"?

python - 当多个 worker 完成未知尺寸的工作时如何收到通知?

python - Skype4py 连接不起作用

Python 解压缩文件并连接结果

php - 从 PHP 脚本将数据传递给 Python Web 爬虫

python - Django ORM 中的分组依据

python - 用于查找字体系列的 CSS 解析 Python