要获取标记内的文本,有几种方法,
a)使用标记的.text属性.
cars = soup.find_all('span',attrs={'class': 'listing-row__price'})
for tag in cars:
print(tag.text.strip())
产量
$71,996
$75,831
$71,412
$75,476
....
for tag in cars:
print(tag.get_text().strip())
c)如果标签内只有该字符串,您也可以使用这些选项
> .string
> .contents [0]
>下一个(tag.children)
> next(tag.strings)
> next(tag.stripped_strings)
即.
for tag in cars:
print(tag.string.strip()) #or uncomment any of the below lines
#print(tag.contents[0].strip())
#print(next(tag.children).strip())
#print(next(tag.strings).strip())
#print(next(tag.stripped_strings))
输出:
$71,476
$77,001
...
注意:
.text和.string不一样.如果标记中有其他元素,则.string返回None,而.text将返回标记内的文本.
from bs4 import BeautifulSoup
html="""
hello there
"""
soup = BeautifulSoup(html,'html.parser')
p = soup.find('p')
print(p.string)
print(p.text)
输出
None
hello there