请教如何从网页返回字符串中提取json字符串部分
网页返回这样的
/<script>location.href='//sina.com';</script>/
IO.XSRV2.CallbackList['18LBE3q_rMW3lVH$'] ({"count":"9884","data":[{"name":"United Parcel Service, Inc.","cname":"u8054u5408u5305u88f9u670du52a1u516cu53f8","category":"u8fd0u8f93","symbol":"UPS","price":"123.49","diff":"1.54","chg":"1.26","preclose":"121.95","open":"122.35","high":"124.66","low":"122.02","amplitude":"2.16%","volume":"3156161","mktcap":"109000916414","pe":"21.36505076","market":"NYSE","category_id":"34"},{"name":"GlaxoSmithKline Plc","cname":"u845bu5170u7d20u53f2u514bu516cu53f8","category":"u751fu7269u6280u672fu548cu5236u836f","symbol":"GSK","price":"44.21","diff":"-0.15","chg":"-0.34","preclose":"44.36","open":"44.35","high":"44.36","low":"44.11","amplitude":"0.56%","volume":"1630946","mktcap":"108537758252","pe":"18.97424916","market":"NYSE","category_id":"700"}]});
python如何解析"data"数据?
请高人指点!
回答:
通过字符串提取下面的部分
string = {"count":"9884","data":[{"name":"United Parcel Service, Inc.","cname":"u8054u5408u5305u88f9u670du52a1u516cu53f8","category":"u8fd0u8f93","symbol":"UPS","price":"123.49","diff":"1.54","chg":"1.26","preclose":"121.95","open":"122.35","high":"124.66","low":"122.02","amplitude":"2.16%","volume":"3156161","mktcap":"109000916414","pe":"21.36505076","market":"NYSE","category_id":"34"},{"name":"GlaxoSmithKline Plc","cname":"u845bu5170u7d20u53f2u514bu516cu53f8","category":"u751fu7269u6280u672fu548cu5236u836f","symbol":"GSK","price":"44.21","diff":"-0.15","chg":"-0.34","preclose":"44.36","open":"44.35","high":"44.36","low":"44.11","amplitude":"0.56%","volume":"1630946","mktcap":"108537758252","pe":"18.97424916","market":"NYSE","category_id":"700"}]}
然后,
string2dict = json.loads(string)
这样就把string转化为dict
然后取对应的值就好了
data = string2dict["data"]
以上是 请教如何从网页返回字符串中提取json字符串部分 的全部内容, 来源链接: utcz.com/a/160383.html