Python爬虫response不是标准Json格式,怎么解析?
在爬取http://vip.win007.com/count/goalCount.aspx?t=5&sid=1988099&cid=115,图1中红色方框内数据网页是动态网页,但是response对象不是标准的json格式,如图2所示,有前缀,
怎么把前面的
“var goalPageInfo = ;
var goalPageData =”
信息去掉,留下后面的内容?
import json
import requests
url = 'http://vip.win007.com/count/goalData.aspx?p=1&sp=1&t=5&time=2021/5/28%2019:00:00&hw=2.3&gw=3&g=3&cid=115&r=1&t2=1&scid=466&flesh=0.22358079357785554'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
}
#r = requests.get(url=url,headers=headers).json()
#print(r)
r = requests.get(url=url,headers=headers)
# 调用get方法,下载
#print(type(r.content.decode('gbk')))
#print(r.content.decode('gbk'))
ret = r.content.decode('gbk')
print(ret)
result = json.loads(ret)
print(result)
代码会报错
这就是去掉前面部分也只是列表啊,和json也不沾边 试试ujson库 import requests
url = 'http://vip.win007.com/count/goalData.aspx?p=1&sp=1&t=5&time=2021/5/28%2019:00:00&hw=2.3&gw=3&g=3&cid=115&r=1&t2=1&scid=466&flesh=0.22358079357785554'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"
}
r = requests.get(url=url,headers=headers)
r.encoding="gbk"
lst=r.text.split("=")[:-2]
for x in eval(lst):
print(x) import requests
import re
url = 'http://vip.win007.com/count/goalData.aspx?p=1&sp=1&t=5&time=2021/5/28%2019:00:00&hw=2.3&gw=3&g=3&cid=115&r=1&t2=1&scid=466&flesh=0.22358079357785554'
headers = {
'User-Agent': 'Mozilla/5.0'
}
res = requests.get(url, headers=headers)
datas = eval(re.findall('Data = (.*?);', res.content.decode('gbk')))
for data in datas:
print(data) import re
import requests
res = requests.get(
url='http://vip.win007.com/count/goalData.aspx?p=1&sp=1&t=5&time=2021/5/28%2019:00:00&hw=2.3&gw=3&g=3&cid=115&r=1&t2=1&scid=466',
headers={'User-Agent': 'Mozilla/5.0' }
)
m = re.findall("(\[\[.*?\]\])",res.content.decode('gbk'), re.S|re.M)
forl in eval(m):
print(l)
页:
[1]