Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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在html标记后获取文本_Python_Html_Beautifulsoup - Fatal编程技术网

Python BeautifulSoup在html标记后获取文本

Python BeautifulSoup在html标记后获取文本,python,html,beautifulsoup,Python,Html,Beautifulsoup,我有以下html,我想得到Name后面的泰语文本,也就是:这就是我想要的 content = """ <html><body><b>Name of Bangkok Bus station:</b> <span itemprop="name">Victory Monument</span> <meta content="http://www.transitbangkok.com/stations/Bangkok%20Bu

我有以下
html
,我想得到
Name后面的泰语文本,也就是
:这就是我想要的

content = """
<html><body><b>Name of Bangkok Bus station:</b>
<span itemprop="name">Victory Monument</span>
<meta content="http://www.transitbangkok.com/stations/Bangkok%20Bus/Victory%20Monument" itemprop="url"/>
<meta content="http://www.transitbangkok.com/stations/Bangkok%20Bus/Victory%20Monument" itemprop="map"/>
<br/><b>Name in Thai</b>: this is what i want<br/>
</body></html>
"""
但是,我得到了
\n
作为输出。有没有一种方法可以在特定的标记后获得文本(有解释就好了!)

但是,我得到了
\n
作为输出

这是因为
find(“b”)
返回它遇到的第一个
标记,在
内容中的第一个标记之后,只有一个换行符

如果您遍历所有
标记。然后您将看到
next_sibling
为您提供了所需:

for tag in soup.find_all("b"):
    print(tag.text)
    print(tag.next_sibling)
输出:

Name of Bangkok Bus station:


Name in Thai
: this is what i want
您可以通过使用空格中的
strip()
'ing
next\u sibling
遍历它们并找到一个后面有内容的

for tag in soup.find_all("b"):
    after = tag.next_sibling.strip()
    if after:
        print(tag.next_sibling)
但是,我得到了
\n
作为输出

这是因为
find(“b”)
返回它遇到的第一个
标记,在
内容中的第一个标记之后,只有一个换行符

如果您遍历所有
标记。然后您将看到
next_sibling
为您提供了所需:

for tag in soup.find_all("b"):
    print(tag.text)
    print(tag.next_sibling)
输出:

Name of Bangkok Bus station:


Name in Thai
: this is what i want
您可以通过使用空格中的
strip()
'ing
next\u sibling
遍历它们并找到一个后面有内容的

for tag in soup.find_all("b"):
    after = tag.next_sibling.strip()
    if after:
        print(tag.next_sibling)