我在解析方面有点问题。我有身体=
<div class='contact'>
<i class='fa fa phone'></i>
Text what I want
</div>
我有很多类似的代码。我用=
def get_html(url):
response = requests.get(url)
return response.text
def get_all_links(html):
soup = BeautifulSoup(html, "html.parser")
tds = soup.find('div', class_='col-xs-7 insInfoRow onlyGal')
link = []
for i in tds:
link.append(i)
return link
我只想要此文本。帮助请
答案 0 :(得分:0)
适合您的示例代码。 .text
可以直接从element获取文本内容。
from bs4 import BeautifulSoup
html="""
<div class='contact'>
<i class='fa fa phone'></i>
Text what I want
</div>
<div class='contact'>
<i class='fa fa phone'></i>
Text what I want too
</div>
"""
soup = BeautifulSoup(html, "html.parser")
divs = soup.find_all('div', class_='contact')
print([div.text.strip() for div in divs])
PS:.strip()
可以删除空格。