首页 > 解决方案 > BeautifulSoup如何解析没有标签的元素

问题描述

我对解析有一点问题。我有身体=

<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

我只想要这个文本。请帮助

标签: pythonparsingbeautifulsoup

解决方案


为您提供的示例代码。.text可以直接从元素中获取文本内容。

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()可以去掉空格。


推荐阅读