【发布时间】:2015-12-19 23:36:00
【问题描述】:
我有 JSON 对象:
{
"review_body": "Beef noodles realism weathered modem tanto hotdog dolphin long-chain hydrocarbons 8-bit euro-pop tank-traps Tokyo narrative.-space j-pop franchise otaku faded RAF girl artisanal hotdog denim ablative systemic smart-Kowloon. Man construct dome smart-computer pen monofilament beef noodles rain garage geodesic bicycle San Francisco wonton soup dissident nodal point tower. Boat uplink film dead man modem warehouse. Nodal point jeans euro-pop render-farm nano-fetishism semiotics hacker gang. Futurity narrative youtube otaku Kowloon free-market drugs. Fluidity assassin Tokyo bicycle media assault concrete industrial grade ablative lights boat BASE jump A.I. post-stimulate carbon. Physical computer narrative city youtube math-neural assassin modem.",
"link": "http://www.getlost.com/store/acme/review/10607787#comment10607787",
"seller_id": "104523",
"survey_id": "9933447",
"loggedin_user": 0,
"store_rating": "8.02",
"store_thumb": "http://www.getlost.com/store/thumbnail/acme.jpg",
"store_name": "acme",
"username": "ronin666",
"rating": "1",
"ref": "RR,acme,104523"
}
嵌入
<script LANGUAGE="javascript">
window.commentShare = $.extend((window.commentShare || {}), {
10607787: {
"review_body": "Beef noodles realism weathered modem tanto hotdog dolphin long-chain hydrocarbons 8-bit euro-pop tank-traps Tokyo narrative.-space j-pop franchise otaku faded RAF girl artisanal hotdog denim ablative systemic smart-Kowloon. Man construct dome smart-computer pen monofilament beef noodles rain garage geodesic bicycle San Francisco wonton soup dissident nodal point tower. Boat uplink film dead man modem warehouse. Nodal point jeans euro-pop render-farm nano-fetishism semiotics hacker gang. Futurity narrative youtube otaku Kowloon free-market drugs. Fluidity assassin Tokyo bicycle media assault concrete industrial grade ablative lights boat BASE jump A.I. post-stimulate carbon. Physical computer narrative city youtube math-neural assassin modem.",
"link": "http:\/\/www.getlost.com\/store\/acme\/review\/10607787#comment10607787",
"seller_id": "104523",
"survey_id": "9933447",
"loggedin_user": 0,
"store_rating": "8.02",
"store_thumb": "http:\/\/www.getlost.com\/store\/thumbnail\/acme.jpg",
"store_name": "acme",
"username": "ronin666",
"rating": "1",
"ref": "RR,acme,104523"
}
});
</script>
我想提取上述 JSON 对象。如何做到这一点?我应该使用正则表达式吗?
如何获得这种类型的对象(通过Ipython,python 2.7):
我基本上是在使用 BeautifulSoup 抓取评论网站 resellerratings.com 的任意商店。我获得了 soup 对象,并注意到有一些有用的 JSON 对象包含所选商店中每条评论的信息。但是,在调用 soup.find("script", language = "javascript") 时,我仍然保留了嵌入在脚本标签中的 JSON 对象。
from mechanize import Browser
import bs4
from bs4 import BeautifulSoup
br = Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
example_url = 'http://www.resellerratings.com/store/My_Digital_Palace'
response = br.open(example_url)
soup = BeautifulSoup(response)
soup.find("script", language = "javascript")
这应该返回:
<script language="javascript">
window.commentShare = $.extend(
(window.commentShare || {}), {
375015: {
"review_body": "I bought a Kodak LS443 form My Digital Palace in 2004. I also purchased a 5 year warranty. Now the camera does not work and I am unable to contact them. What do I do??? Am I just screwed???<br><br>Margaret Fuller<br>margaret_fuller@sbcglobal.net",
"link": "http:\/\/www.resellerratings.com\/store\/My_Digital_Palace\/review\/375015#comment375015",
"seller_id": "6930",
"survey_id": "385176",
"loggedin_user": 0,
"store_rating": "1.00",
"store_thumb": "http:\/\/www.resellerratings.com\/store\/thumbnail\/My_Digital_Palace.jpg",
"store_name": "My Digital Palace",
"username": "maf1059",
"rating": "1",
"ref": "RR,My_Digital_Pala,6930"
}
}
);
</script>
【问题讨论】:
-
您是否将其视为您要解析的常规文本文件,还是包含在您的网络应用程序中?
-
@ergonaut 这实际上是我在 python 中使用美丽的汤刮的东西。所以这是我想要解析的东西。
-
为什么不简单地通过变量访问对象?
-
更多信息会有所帮助...您想在 python 中解析?还是javascript?是 $.extend 调用 jquery 还是什么?
-
就目前而言,这听起来像是您正在尝试做一些非常错误的事情。
标签: python json regex web-scraping beautifulsoup