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