如何使用Python遍历标签?

3 投票
2 回答
3615 浏览
提问于 2025-04-16 16:33

我想要遍历一些HTML内容,并把数据存储到一个字典里。每次遍历都是从:

<h1 class="docDisplay" id="docTitle">

我有以下代码:

html = '<html><body><h1 class="docDisplay" id="docTitle">Data1</h1><p>other data<\p><h1 class="docDisplay" id="docTitle">Data2</h1><p>other data2<\p></html>'

soup=BeautifulSoup(html)
newdoc = soup.find('h1', id="docTitle")
title = newdoc.findNext(text=True)
data = title.findAllNext('p',text=True)
data_dict = {}
data_dict[title] = {'data': data}
print data_dict

现在,输出结果是:

{u'Data1': {'data': [u'other data<\\p>', u'Data2', u'other data2<\\p>']}}

我希望输出结果是:

{u'Data1': {'data': [u'other data<\\p>']}, u'Data2': {'data': [u'other data2<\\p>']}}

我不知道一旦到达新的h1标签后,应该怎么重新开始。有什么想法吗?

2 个回答

-1

@samplebias: @Lynch说得对。如果提问者没有正确关闭他们的标签,那么就不能指望解析器能读懂他们的心思。

试着修正你的HTML代码,这样可能就能正常工作了。=)

1

为了让每个标题下的段落文本相匹配,我会尝试这样做(你可能需要根据你想要的具体输出格式进行一些调整):

    from BeautifulSoup import BeautifulSoup

    html = """ 
    <html>
    <head>
    </head>

    <body>
      <h1 class="docDisplay" id="docTitle">Data1</h1>
      <p>other data</p>
      <p>Another paragraph under the first heading.</p>
      <h1 class="docDisplay" id="docTitle">Data2</h1>
      <p>other data2</p>
      <div><p>This paragraph is NOT a sibling of the header</p></div>
    </body>
    </html>
"""

soup = BeautifulSoup(html)

data_dict = {}
stuff_under_current_heading = []

firstHeader = soup.find('h1', id="docTitle")
for tag in [firstHeader] + firstHeader.findNextSiblings():
    if tag.name == 'h1':
        stuff_under_current_heading = []
        # I chose to strip excess whitespace from the header name:
        data_dict[tag.string.strip()] = {'data': stuff_under_current_heading}
        # Modifying the list modifies the value in the dictionary.
    # Take every <p> tag encountered between here and the next heading
    # and associate it with the most recently-seen <h1> tag.
    elif tag.name == 'p':
        stuff_under_current_heading.append(tag.string)
    # Include <p> tags that are not siblings of the <h1> tag but
    # are still part of the content under the header.
    else:
        stuff_under_current_heading.extend(tag.findAll('p', text=True))

print data_dict

这样输出的结果是

{u'Data1': {'data': [u'other data', u'Another paragraph under the first heading.']},   
 u'Data2': {'data': [u'other data2', u'This paragraph is NOT a sibling of the header']}}

撰写回答