为什么 BeautifulSoup 将 <html><body><p> 添加到我的结果中?
Why BeautifulSoup add <html><body><p> to my results?
问题
我有以下Page01.htm
<!DOCTYPE html><html lang="it-IT"><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <head><title>Title here</title></head>
<body>
<script id="TargetID" type="application/json"><![CDATA[
{ "name":"Kate", "age":22, "city":"Boston"}
]]>
</script><script id=“AnotherID” type="application/json"><![CDATA[{ "name":"John", "age":31, "city":"New York"}]]>
</script>
</body></html>
我想提取 ID=TargetID
.
脚本标签之间的 JSON 内的信息
我做了什么
我写了下面的Python3.6代码:
from bs4 import BeautifulSoup
import codecs
page_path="/Users/me/Page01.htm"
page = codecs.open(page_path, "r", "utf-8")
soup = BeautifulSoup(page.read(), "lxml")
vegas = soup.find_all(id="TargetID")
invalid_tags = ['script']
soup = BeautifulSoup(str(vegas),"lxml")
for tag in invalid_tags:
for match in soup.findAll(tag):
match.replaceWithChildren()
JsonZ = str(soup)
现在,如果我查看 vegas
变量,我可以看到
[<script id="TargetID" type="application/json"><![CDATA[ {
> "name":"Kate", "age":22, "city":"Boston"} ]]> </script>]
但是如果我尝试删除脚本标签(使用 this answer 脚本),我会得到以下 JsonZ
变量
'<html><body><p>[<![CDATA[\n{ "name":"Kate", "age":22, "city":"Boston"}\n]]>\n]</p></body></html>'
没有脚本标签但有另外 3 个标签 (<html><body><p>
) 完全没用。
我的目标是使用 Python JSON 模块加载以下字符串 { "name":"Kate", "age":22, "city":"Boston"}
。
BeautifulSoup 几乎可以接受任何东西,并尝试将其转换为 HTML 的完整页面。这就是您收到 '<html><body> ...'
的原因。通常这是一件好事,因为 HTML 的格式可能很糟糕,但 BeautifulSoup 仍会处理它。
在您的情况下,提取 json 的一种方法是这样的。
>>> import bs4
>>> page = bs4.BeautifulSoup(open('Page01.htm').read(), 'lxml')
>>> first_script = page.select('#TargetID')[0].text
>>> first_script
'<![CDATA[\n{ "name":"Kate", "age":22, "city":"Boston"}\n]]>\n'
>>> content = first_script[first_script.find('{'): 1+first_script.rfind('}')]
>>> content
'{ "name":"Kate", "age":22, "city":"Boston"}'
一旦你有了这个,你就可以把它变成一个 Python 字典,就像这样。
>>> import json
>>> d = json.loads(content)
>>> d['name']
'Kate'
>>> d['age']
22
>>> d['city']
'Boston'
它可以变得更简单:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("/Users/me/Page01.htm", encoding='utf-8'), "html.parser")
result = soup.find('script', type='application/json', id='TargetID').text
# Workaround to get CDATA content (It seems that it can't be done with bs):
result = result.replace("<![CDATA[", "").replace("]]>", "").strip()
问题
我有以下Page01.htm
<!DOCTYPE html><html lang="it-IT"><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <head><title>Title here</title></head>
<body>
<script id="TargetID" type="application/json"><![CDATA[
{ "name":"Kate", "age":22, "city":"Boston"}
]]>
</script><script id=“AnotherID” type="application/json"><![CDATA[{ "name":"John", "age":31, "city":"New York"}]]>
</script>
</body></html>
我想提取 ID=TargetID
.
我做了什么
我写了下面的Python3.6代码:
from bs4 import BeautifulSoup
import codecs
page_path="/Users/me/Page01.htm"
page = codecs.open(page_path, "r", "utf-8")
soup = BeautifulSoup(page.read(), "lxml")
vegas = soup.find_all(id="TargetID")
invalid_tags = ['script']
soup = BeautifulSoup(str(vegas),"lxml")
for tag in invalid_tags:
for match in soup.findAll(tag):
match.replaceWithChildren()
JsonZ = str(soup)
现在,如果我查看 vegas
变量,我可以看到
[<script id="TargetID" type="application/json"><![CDATA[ {
> "name":"Kate", "age":22, "city":"Boston"} ]]> </script>]
但是如果我尝试删除脚本标签(使用 this answer 脚本),我会得到以下 JsonZ
变量
'<html><body><p>[<![CDATA[\n{ "name":"Kate", "age":22, "city":"Boston"}\n]]>\n]</p></body></html>'
没有脚本标签但有另外 3 个标签 (<html><body><p>
) 完全没用。
我的目标是使用 Python JSON 模块加载以下字符串 { "name":"Kate", "age":22, "city":"Boston"}
。
BeautifulSoup 几乎可以接受任何东西,并尝试将其转换为 HTML 的完整页面。这就是您收到 '<html><body> ...'
的原因。通常这是一件好事,因为 HTML 的格式可能很糟糕,但 BeautifulSoup 仍会处理它。
在您的情况下,提取 json 的一种方法是这样的。
>>> import bs4
>>> page = bs4.BeautifulSoup(open('Page01.htm').read(), 'lxml')
>>> first_script = page.select('#TargetID')[0].text
>>> first_script
'<![CDATA[\n{ "name":"Kate", "age":22, "city":"Boston"}\n]]>\n'
>>> content = first_script[first_script.find('{'): 1+first_script.rfind('}')]
>>> content
'{ "name":"Kate", "age":22, "city":"Boston"}'
一旦你有了这个,你就可以把它变成一个 Python 字典,就像这样。
>>> import json
>>> d = json.loads(content)
>>> d['name']
'Kate'
>>> d['age']
22
>>> d['city']
'Boston'
它可以变得更简单:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("/Users/me/Page01.htm", encoding='utf-8'), "html.parser")
result = soup.find('script', type='application/json', id='TargetID').text
# Workaround to get CDATA content (It seems that it can't be done with bs):
result = result.replace("<![CDATA[", "").replace("]]>", "").strip()