\试图从此html中获取文本A Plus和计算机:
<div class="u-space-t1">
<h1 class="biz-page-title embossed-text-white shortenough">A Plus</h1>
<div class="u-inline-block">
<h1 class="biz-page-title\ embossed-text-white\ shortenough">Computers</h1>
<div class="u-inline-block">
所以我试图得到这样的文本:
c = soup.findAll('h1',{"class":"biz-page-title embossed-text-white shortenough"})
print(c)
但是我有一个空列表
我也尝试过这样做:
c = soup.find('div', class_='u-inline-block').h1
我找不到一个“ Nonetype”对象。
答案 0 :(得分:1)
这样做吧。
texts = soup.select("div > h1, div > div > h1")
for text in texts:
print(text.text)
“ A Plus”和“计算机”将会出现。
答案 1 :(得分:1)
尝试一下:
html = """
<div class="u-space-t1">
<h1 class="biz-page-title embossed-text-white shortenough">A Plus</h1>
<div class="u-inline-block">
<h1 class="biz-page-title\ embossed-text-white\ shortenough">Computers</h1>
<div class="u-inline-block">
"""
soup = bs4(html, 'lxml')
for i in soup.find_all('h1'):
print(i.text)
输出:
A Plus
Computers