Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用Beautifulsoup获取嵌套标记的文本?_Python_Html_Web Scraping - Fatal编程技术网

如何在Python中使用Beautifulsoup获取嵌套标记的文本?

如何在Python中使用Beautifulsoup获取嵌套标记的文本?,python,html,web-scraping,Python,Html,Web Scraping,运行此代码之后 section=soup.find_all('section',class='b-branchs') 我明白了 坚定的RJT路线 现在我只想提取RJIT道路线而不是…坚定 所以我试过了 对于部分中的i:firm=i.find('div',class='b-branchs\uu item')打印(firm) 它将返回公司和RJIT路线 那么,如何只提取div标记的文本呢?您可以使用tag.contents[1]来获得预期的输出 示例: from bs4 import Beautif

运行此代码之后

section=soup.find_all('section',class='b-branchs')

我明白了

坚定的RJT路线

现在我只想提取RJIT道路线而不是…坚定

所以我试过了

对于部分中的i:firm=i.find('div',class='b-branchs\uu item')打印(firm)

它将返回公司和RJIT路线


那么,如何只提取div标记的文本呢?

您可以使用
tag.contents[1]
来获得预期的输出

示例:

from bs4 import BeautifulSoup

html = """
<div class="b-branches__item"><i class="icon fa"><b>Firm</b> </i>RJT Roadlines</div>
"""

soup=BeautifulSoup(html,'html.parser')

tag = soup.find('div', class_='b-branches__item')
print(tag.contents[1])
RJT Roadlines

非常感谢…很高兴能提供帮助,欢迎使用Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受-假设在使用print(tag.contents[1])后,我得到了输出,即9879887。现在,如何将它们存储在两个不同的变量x1,x2中。不清楚您的意思,您应该用完整的示例作为新问题来提问。如果我理解正确,只需将其赋值为
x1=soup.find('div',class='b-branchs\uu item')。内容[1]
欢迎使用堆栈溢出。在请求调试帮助时,我们要求您通过生成一个。这有助于人们理解您的问题,而不必通读整个代码。