首页 > Beautiful Soup这个库有没有人使用?使用的人多吗?

Beautiful Soup这个库有没有人使用?使用的人多吗?

f=open('zbl.html','r')
htm=f.read()
htm=unicode(htm,chardet.detect(htm)['encoding'])
soup=bs(htm,'lxml')
content=soup.select('.viewbox .content')[0]
imgs=content.find_all('img')
for img in imgs:
    print(img.get('src'))

zbl.html是我复制这个网页内容的源http://www.centoscn.com/CentOS/2015/0813/5995.html,
网页的内容里面有两个图片,我无论使用find_all方法还是select方法,始终都只能得到一个图片,不知道是怎么回事?
我的环境是win7+python2.7+bs4,不知是这个库不成熟,还是我的代码有点问题,求帮忙看看。

下面是命令行的输出,只有一个URL。


这个库用的人还不少吧应该,我也是初学者不少人都有推荐过这个库,我给出我自己刚刚看文档写的

import urllib.request
from bs4 import BeautifulSoup
url='http://www.centoscn.com/CentOS/2015/0813/5995.html'
content=urllib.request.urlopen(url).read().decode('gbk')
soup=BeautifulSoup(content)
contents=soup.find_all('img',attrs={'src':True,'style':True})
for content in contents:
    print(content.get('src'))

我用的是python3.4
你的代码在我的环境下是没有问题的

import urllib.request
from bs4 import BeautifulSoup
url='http://www.centoscn.com/CentOS/2015/0813/5995.html'
content=urllib.request.urlopen(url).read().decode('gbk')
soup=BeautifulSoup(content)
content=soup.select('.viewbox .content')[0]
imgs=content.find_all('img')
for img in imgs:
    print(img.get('src'))

可能是你的解析器的问题,将lxml换成默认的解析器看看

【热门文章】
【热门文章】