16

使用 Beautiful Soup 模块,如何获取div类名为的标签的数据feeditemcontent cxfeeditemcontent?是吗:

soup.class['feeditemcontent cxfeeditemcontent']

或者:

soup.find_all('class')

这是 HTML 源代码:

<div class="feeditemcontent cxfeeditemcontent">
    <div class="feeditembodyandfooter">
         <div class="feeditembody">
         <span>The actual data is some where here</span>
         </div>
     </div>
 </div> 

这是Python代码:

 from BeautifulSoup import BeautifulSoup
 html_doc = open('home.jsp.html', 'r')

 soup = BeautifulSoup(html_doc)
 class="feeditemcontent cxfeeditemcontent"
4

6 回答 6

24

Beautiful Soup 4 将 "class" 属性的值视为列表而不是字符串,这意味着 jadkik94 的解决方案可以简化:

from bs4 import BeautifulSoup                                                   

def match_class(target):                                                        
    def do_match(tag):                                                          
        classes = tag.get('class', [])                                          
        return all(c in classes for c in target)                                
    return do_match                                                             

soup = BeautifulSoup(html)                                                      
print soup.find_all(match_class(["feeditemcontent", "cxfeeditemcontent"]))
于 2012-07-05T14:22:08.517 回答
11

试试这个,也许这个简单的东西太多了,但它有效:

def match_class(target):
    target = target.split()
    def do_match(tag):
        try:
            classes = dict(tag.attrs)["class"]
        except KeyError:
            classes = ""
        classes = classes.split()
        return all(c in classes for c in target)
    return do_match

html = """<div class="feeditemcontent cxfeeditemcontent">
<div class="feeditembodyandfooter">
<div class="feeditembody">
<span>The actual data is some where here</span>
</div>
</div>
</div>"""

from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(html)

matches = soup.findAll(match_class("feeditemcontent cxfeeditemcontent"))
for m in matches:
    print m
    print "-"*10

matches = soup.findAll(match_class("feeditembody"))
for m in matches:
    print m
    print "-"*10
于 2012-07-04T15:16:49.963 回答
9

soup.findAll("div", class_="feeditemcontent cxfeeditemcontent")

因此,如果我想<div class="header">从 stackoverflow.com 获取类头的所有 div 标签,BeautifulSoup 的示例将类似于:

from bs4 import BeautifulSoup as bs
import requests 

url = "http://stackoverflow.com/"
html = requests.get(url).text
soup = bs(html)

tags = soup.findAll("div", class_="header")

它已经在 bs4文档中。

于 2014-07-24T05:29:55.420 回答
5
from BeautifulSoup import BeautifulSoup 
f = open('a.htm')
soup = BeautifulSoup(f) 
list = soup.findAll('div', attrs={'id':'abc def'})
print list
于 2013-02-16T06:26:47.913 回答
3
soup.find("div", {"class" : "feeditemcontent cxfeeditemcontent"})
于 2012-07-04T14:55:52.827 回答
0

检查此错误报告:https ://bugs.launchpad.net/beautifulsoup/+bug/410304

如您所见,Beautiful soup 并不能真正理解class="a b"为两个类ab.

但是,正如第一条评论中出现的那样,一个简单的正则表达式就足够了。在你的情况下:

soup = BeautifulSoup(html_doc)
for x in soup.findAll("div",{"class":re.compile(r"\bfeeditemcontent\b")}):
    print "result: ",x

注意:这已在最近的测试版中得到修复。我没有浏览最新版本的文档,也许你可以这样做。或者,如果您想使用旧版本使其正常工作,则可以使用上述方法。

于 2012-07-04T14:56:05.863 回答