使用BeautifulSoup在流文本中刮取B

2024-09-29 17:14:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下bs4元素标记:

<span><span>some content</span> B</span>

字符串B的长度未知(为了简化,我将其命名为B)

如何使用beautifulSoup提取“b”?或者我只需要提取文本,然后使用一些regexp技术作为解决方案

谢谢

编辑:完整的代码

def get_doc_yakarouler(license_plate,url = 'https://www.yakarouler.com/car_search/immat?immat='):
    response = requests.get(url+license_plate)
    content = response.content 
    doc = BeautifulSoup(content,'html.parser')
    result = doc.span.text
    if 'identifié' in result :
        return doc
    else : 
        return f"La plaque {license_plate} n'est pas recensé sur yakarouler"

doc = get_doc_yakarouler('AA300AA')
span = doc.find_all('span')
motorisation_tag = span[1]

我想提取“1.6 TDI”

我使用以下方法找到了解决方案:motorization_tag.text.replace(u'\xa0','').split('')[1],但我想知道是否可以直接使用bs4


Tags: texturlgetdocreturnlicenseresponseresult
2条回答
from bs4 import BeautifulSoup as bs , NavigableString
html = '<span><span>some content</span> B</span>'
soup = bs(html, 'html.parser')
span = soup.find("span")
# First approach Using Regular Expressions
outer_text_1 = span.find(text=True, recursive=False)
# Second approach is looping through the contents of the tag and check if it's the outer text and not a tag
outer_text_2 = ' '.join([t for t in span.contents if type(t)== NavigableString])

print(outer_text_1) # output B
print(outer_text_2) # output B

假设您有一个变量span,它表示外部<span>标记,您可以执行以下操作来提取“B”:span.contents[1]。这是因为.contents将返回标记内容的列表,在本例中为[<span>some content</span>, ' B']。然后可以访问“B”文本作为数组的第二个元素。请注意,如果B前面有空格,如HTML示例中所示,则该空格将包含在字符串中

相关问题 更多 >

    热门问题