3 回答
TA贡献2039条经验 获得超7个赞
要抓取所有值,您只需要获取所有li标签(不限制搜索具有类的元素,cell036 tal arrow这就是为什么您只能获取该值):
试试这个:
from bs4 import BeautifulSoup
html_text = """
<ul>
<li class="cell036 tal arrow"><a href=" y/">ALdCTL</a></li>
<li class="cell009">5,71</li>
<li class="cell009">5,74</li>
<li class="cell009">-3,04</li>
<li class="cell009">5,92</li>
<li class="cell009">5,76</li>
<li class="cell009">5,53</li>
<li class="cell009">907.438</li>
<li class="cell009">5.114.192</li>
</ul>
"""
soup = BeautifulSoup(html_text, "lxml")
for foo in soup.find_all('li'):
print(foo.text)
输出:
ALdCTL
5,71
5,74
-3,04
5,92
5,76
5,53
907.438
5.114.192
TA贡献1752条经验 获得超4个赞
其他li元素不包含里面的li你正在寻找之中。他们是兄弟姐妹。使用find_next_siblings:
content = """
<ul>
<li class="cell036 tal arrow"><a href=" y/">ALdCTL</a></li>
<li class="cell009">5,71</li>
<li class="cell009">5,74</li>
<li class="cell009">-3,04</li>
<li class="cell009">5,92</li>
<li class="cell009">5,76</li>
<li class="cell009">5,53</li>
<li class="cell009">907.438</li>
<li class="cell009">5.114.192</li>
</ul>
"""
soup = bs4.BeautifulSoup(content)
header = soup.findAll("li", class_="cell036 tal arrow")
header[0].find_next_siblings("li")
给出:
[<li class="cell009">5,71</li>,
<li class="cell009">5,74</li>,
<li class="cell009">-3,04</li>,
<li class="cell009">5,92</li>,
<li class="cell009">5,76</li>,
<li class="cell009">5,53</li>,
<li class="cell009">907.438</li>,
<li class="cell009">5.114.192</li>]
添加回答
举报