我正在尝试从下面发布的html页面获取文本。我试过循环,但它不按顺序打印字符串。我需要按照以下顺序打印字符串:
text1:text_1.1
text2:text2.2,2.2
...
我需要获得上面打印的输出。
<ul>
<li>
<b>text1:</b>
<a><a href="search.php?origin=">text_1.1</a>
</li>
<li>
<b>text2</b>
<a href="search.php?origin=">text_2.1</a>
<a href="search.php?origin">text_2.2</a>
</li>
<li>
<b>text4</b>
<a href="search.php?origin=">text_4.1</a>
<a href="search.php?origin=">text_4.2</a>
<a href="search.php?origin=">text_4.3</a>
<a href="search.php?origin=">text_4.4</a>
</li>
<li>
<b>text5</b>
<a href="search.php?origin=">text5.1</a>
</li>
<li>
<b>text6</b>
<a href="search.php?origin=">text6.1</a>
<a href="search.php?origin=">text6.2</a>
<a href="search.php?origin=">text6.3</a>
<li>
<b>text7</b>
<a href="search.php?origin=">text7.1</a>
<font color="green">text7.2</font>
</li>
<li>
<b>text8</b>
<a href="dwres.php?resource=">2 </a>
</ul>
答案 0 :(得分:0)
from bs4 import BeautifulSoup
for t in BeautifulSoup(html).find("ul").find_all("a"):
print(t.text)
输出:
text_1.1
text_2.1
text_2.2
text_4.1
text_4.2
text_4.3
text_4.4
text5.1
text6.1
text6.2
text6.3
text7.1
如果你想要a和b标签文本:
ul = BeautifulSoup(html).find("ul")
b= [b.text for b in ul.find_all("b")]
a = [a.text for a in ul.find_all("a")]
你需要决定你希望输出如何匹配你,因为显然有更多的标签然后是b。
您还可以获取li标签并使用join访问a和b标签,以获得您想要的内容:
ul = BeautifulSoup(html).find("ul")
li = ul.find_all("li")
for ele in li:
print("{}:{}".format(ele.b.text,"".join([a.text for a in ele.find_all("a")])))
text1::text_1.1
text2:text_2.1text_2.2
text4:text_4.1text_4.2text_4.3text_4.4
text5:text5.1
text6:text6.1text6.2text6.3
text7:text7.1
text8:2
答案 1 :(得分:0)
查找所有<li>
元素,以便您可以按<b>
标记对其内容进行分组。您可能希望字典映射它们,但为了保留文档顺序,您可以使用collections.OrderedDict()
对象:
from collections import OrderedDict
results = OrderedDict()
for li in soup.find_all('li'):
bold = li.b
if bold is None:
continue
results[bold.get_text(strip=True)] = [
link.get_text(strip=True) for link in li.find_all('a')
]
演示:
>>> from bs4 import BeautifulSoup
>>> from collections import OrderedDict
>>> soup = BeautifulSoup('''\
... <ul>
... <li>
... <b>text1:</b>
... <a><a href="search.php?origin=">text_1.1</a>
... </li>
... <li>
... <b>text2</b>
... <a href="search.php?origin=">text_2.1</a>
... <a href="search.php?origin">text_2.2</a>
... </li>
... <li>
... <b>text4</b>
... <a href="search.php?origin=">text_4.1</a>
... <a href="search.php?origin=">text_4.2</a>
... <a href="search.php?origin=">text_4.3</a>
... <a href="search.php?origin=">text_4.4</a>
... </li>
... <li>
... <b>text5</b>
... <a href="search.php?origin=">text5.1</a>
... </li>
... <li>
... <b>text6</b>
... <a href="search.php?origin=">text6.1</a>
... <a href="search.php?origin=">text6.2</a>
... <a href="search.php?origin=">text6.3</a>
... <li>
... <b>text7</b>
... <a href="search.php?origin=">text7.1</a>
... <font color="green">text7.2</font>
... </li>
... <li>
... <b>text8</b>
... <a href="dwres.php?resource=">2 </a>
... </ul>
... ''')
>>> results = OrderedDict()
>>> for li in soup.find_all('li'):
... bold = li.b
... if bold is None:
... continue
... results[bold.get_text(strip=True)] = [
... link.get_text(strip=True) for link in li.find_all('a')
... ]
...
>>> results
OrderedDict([(u'text1:', [u'', u'text_1.1']), (u'text2', [u'text_2.1', u'text_2.2']), (u'text4', [u'text_4.1', u'text_4.2', u'text_4.3', u'text_4.4']), (u'text5', [u'text5.1']), (u'text6', [u'text6.1', u'text6.2', u'text6.3']), (u'text7', [u'text7.1']), (u'text8', [u'2'])])
>>> for key, elems in results.items():
... print '{}: {}'.format(key, ', '.join(elems))
...
text1:: , text_1.1
text2: text_2.1, text_2.2
text4: text_4.1, text_4.2, text_4.3, text_4.4
text5: text5.1
text6: text6.1, text6.2, text6.3
text7: text7.1
text8: 2
print
可以集成到循环中,但通过构建字典,您现在可以进行进一步处理;将其写入文件,将其发送到其他地方等等。