如何使用python Beautiful汤删除开始和结束标记



我很难从json url中剥离开始和结束标记。我用过漂亮的汤,我面临的唯一问题是我的回复中有<pre>标签。请告知我如何删除开始和结束标签。我使用的代码块在这里:

page = Page( "link to json")
soup = bs.BeautifulSoup(page.html, "html.parser")
#fetching the response i want from the url it's inside pre tags.
json = soup.find("pre")
print(json)

感谢黛米安·沃尔夫。解决方案是这样的:

page = Page( "link to json")
soup = bs.BeautifulSoup(page.html, "html.parser")
#fetching the response i want from the url it's inside pre tags.
json = soup.find("pre")
print(json.text)

您可以使用soup.text删除所有标签:

from bs4 import BeautifulSoup

soup = BeautifulSoup("<pre>Hello, world!</pre>", "html.parser")
print(soup.find("pre").text)

最新更新