使用 BeautifulSoup 提取跨度文本

Using BeautifulSoup to extract span text

我有这个代码:

soup = BeautifulSoup(driver.page_source, 'html.parser')
text_intro = soup.find( 'div', { 'id' : 'gerais' } )
usernam = soup.select('#main > aside > div > div:nth-child(2) > span:nth-child(3) ')

print( '', 'Result_text_intro -> ', texto_intro )
print( '', 'Result_usernam -> ', usernam )

它打印这个:

Result_usernam ->  [<span style="color:#F5B518;font-weight:bold"> (TestUser)</span>]

我怎样才能只从跨度中获取“(TestUser)”?

你试过“文本”了吗?

print( '', 'Result_usernam -> ', usernam.text)

你得到 [ ] 所以你有列表,你必须使用 for-loop 单独处理每个元素或 [0] 只得到第一个元素。

然后你可以使用.get_text().string
(在一些教程中你可以看到 .text.string 在较新的版本中是首选)

print('Result_usernam -> ', usernam[0].get_text(strip=True) )

print('Result_usernam -> ', usernam[0].string.strip() )

如果你得到更多的元素

for item in usernam:
    print('Result_usernam -> ', item.get_text(strip=True) )

for item in usernam:
    print('Result_usernam -> ', item.string.strip() )

文档:.get_text() and .string


select() 总是给出列表(即使没有结果或只有一个结果)但也有 select_one() 只得到第一个结果然后你不需要 [0]for-loop.