【问题标题】:How to extract tags from html, json?如何从 html、json 中提取标签?
【发布时间】:2020-03-12 07:24:20
【问题描述】:

我有一个 json 文件,我需要从 json 文件中提取某些 html 标签。

我在下面有一个提示...`

【问题讨论】:

标签: python html json


【解决方案1】:

你可以试试这样的:

try:
    from BeautifulSoup import BeautifulSoup
except ImportError:
    from bs4 import BeautifulSoup

#example of json data (dictionary)
data = {
    "url":"www.example.com",
    "content" : "<!DOCTYPE html><html><body><h1>My First Heading</h1><strong>My first paragraph.</strong></body></html>"
}

#html parser
soup = BeautifulSoup(data["content"], "html.parser")

#get all the tags of one type
print(soup.find_all("h1"))
print(soup.find_all("strong"))

你会得到一个标签列表,比如:

[<h1>My First Heading</h1>]
[<strong>My first paragraph.</strong>]

安装 bs4:

pip install bs4

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多