python爬虫python核心编程第二版

python爬虫:urllib包

2016-08-12  本文已影响56人  Bioconductor
#!/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中运行上面的代码会有警告

使用警告
#!/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'])
上一篇 下一篇

猜你喜欢

热点阅读