import requests
comments = requests.get('http://comment5.news.sina.com.cn/cmnt/count?format=json&\
newslist=gn:comos-hitesuz7540866:0&callback=jQuery111101457597292487176_1536305761369')
ct1 = comments.text
print(ct1)
输出:try{jQuery111101457597292487176_1536305761369({"result": {"status": {"msg": "", "code": 0}, "count": {"gn:comos-hitesuz7540866:0": {"qreply": 1672, "total": 1879, "show": 20}}, "language": "ch", "encoding": "utf-8"}})}catch(e){}
目的:删除“try{jQuery111101457597292487176_1536305761369(”以及“)}catch(e){}”
最终留下:{"result": {"status": {"msg": "", "code": 0}, "count": {"gn:comos-hitesuz7540866:0": {"qreply": 1672, "total": 1879, "show": 20}}, "language": "ch", "encoding": "utf-8"}} 这种结果才是我想要的 这部分可以使用json
在程序编写中使用了strip中的lstrip()和rstrip() 但是出现了多删除的错误,多删除了一对{}号
我的程序:
import requests
comments = requests.get('http://comment5.news.sina.com.cn/cmnt/count?format=json&\
newslist=gn:comos-hitesuz7540866:0&callback=jQuery111101457597292487176_1536305761369')
import json
ct1 = comments.text.lstrip(' try{jQuery111101457597292487176_1536305761369(').rstrip(')}catch(e){}')
print(ct1)
输出:"result": {"status": {"msg": "", "code": 0}, "count": {"gn:comos-hitesuz7540866:0": {"qreply": 1672, "total": 1879, "show": 20}}, "language": "ch", "encoding": "utf-8" 没有了{} 因此就构不成字典,使用json也就不能读取了
请问我的程序哪儿错了,帮我看看,谢谢!!!