前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python包urllib名称

python包urllib名称

作者头像
努力在北京混出人样
发布2019-02-18 14:39:55
9490
发布2019-02-18 14:39:55
举报
  • 在学习《Web Scraping with Python》,发现第32页下面这段代码报错。这段代码的意思是将某个页面全部爬下来。
#!/usr/bin/env python
#coding=utf8

from urllib.request import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://en.wikipedia.org/wiki/Kevin_Bacon")
bsObj = BeautifulSoup(html)
for link in bsObj.findAll("a"):
    if "href" in link.attrs:
        print (link.attrs['href'])

经查询,在python3.5版本中是使用urllib.request,而在python2.7中则是urllib2

修改后,在python2.7中运行上面的代码会有警告

使用警告
使用警告
  • 这里的警告,并不是说有错误,只是习惯在使用时加上lxml 修改后
#!/usr/bin/env python
#coding=utf8

from urllib2 import urlopen
from bs4 import BeautifulSoup

html = urlopen("http://en.wikipedia.org/wiki/Kevin_Bacon")
bsObj = BeautifulSoup(html,"lxml")
for link in bsObj.findAll("a"):
    if "href" in link.attrs:
        print (link.attrs['href'])
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年08月12日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档