首页 > 编程语言 > 详细

Python简单网页爬虫

时间:2017-05-20 14:13:42      阅读:310      评论:0      收藏:0      [点我收藏+]

由于Python2.x与Python3.x存在很的差异,Python2.x调用urllib用指令urllib.urlopen(),

运行时报错:AttributeError: module ‘urllib‘ has no attribute ‘urlopen‘

原因是在Python3.X中应该用urllib.request。

下载网页成功后,调用webbrowsser模块,输入指令webbrowsser

.open_new_tab(‘baidu.com.html‘)

true

open(‘baidu.com.html‘,‘w’)。write(html)

将下载的网页写入指定的目录下,然而下载的网页占0KB,打开显示空白,然后将上代码改为

open(‘baidu.com.html‘,‘wb’)。write(html)

就可以打开了

import urllib
>>> import urllib.request
>>> def getHtml(url):
    page = urllib.request.urlopen(url)
    html = page.read()
    return html

>>> import webbrowser
>>> webbrowser.open_new_tab(baidu.com.html)
True
>>> open(baidu.com.html,wb).write(html)

 

Python简单网页爬虫

原文:http://www.cnblogs.com/dfguo/p/6882059.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!