首页 > 代码库 > Python简单网页爬虫
Python简单网页爬虫
由于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简单网页爬虫
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。