Python读取网页内容显示问题

黑夜愁客 2011-07-31 08:29:54
我想读取土豆网站的一个页面地址,比如http://www.tudou.com/programs/view/kS03BynGs8Q
但是
>>> req = urllib2.Request('http://www.tudou.com/programs/view/kS03BynGs8Q')
>>> req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0)')
>>> page = urllib2.urlopen(req)
>>> data = page.read()
>>> print data

>>> print len(data)
7202

为何打印不出网页html内容呢,而且长度却是7202呢
...全文
464 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
I_NBFA 2011-08-03
  • 打赏
  • 举报
回复
这种问题先抓包
zenxiaoxian 2011-08-03
  • 打赏
  • 举报
回复
import urllib,re
url ='http://www.tudou.com/programs/view/kS03BynGs8Q'
wp = urllib.urlopen(url).read()
#bug = re.findall(r"<title>*......",wp);
#for mai in bug

print wp


Waistcoat22 2011-08-01
  • 打赏
  • 举报
回复
应该是网站返回的内容为gzip压缩格式,但我测试不是每次都返回gzip格式,所以加了个判断:


import urllib2
import StringIO
import gzip

url = 'http://www.tudou.com/programs/view/kS03BynGs8Q'
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0)')
response = urllib2.urlopen(req)
content = response.read()
response.close()

html = ""
if response.headers["Content-Encoding"] == 'gzip':
html = gzip.GzipFile(fileobj = StringIO.StringIO(content)).read()
else:
html = content

print html
freetstar 2011-08-01
  • 打赏
  • 举报
回复
编码的问题吧,试试
#-*- coding=utf-8 -*-
BoredNight 2011-08-01
  • 打赏
  • 举报
回复
debug一下看看data的内容
黑夜愁客 2011-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 waistcoat22 的回复:]

可以啊,输出不就是一个html文件嘛
[/Quote]那个为何输出有问题呢
Waistcoat22 2011-07-31
  • 打赏
  • 举报
回复
print len(data)
输出结果:31854
Waistcoat22 2011-07-31
  • 打赏
  • 举报
回复
可以啊,输出不就是一个html文件嘛

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧