我是python的新手,我收到错误但我无法弄明白:pics = r.json()['data'['photo_list']
请帮忙解决这个问题,非常感谢!
# -*-coding:utf8-*-
import re
import json
from urllib.request import urlretrieve
import requests
def main():
headers = {
'Cookie' : 'SUB=_2AkMhFc9hf8NhqwJRmPoRym_jaI9_ygvEiebDAHzsJxJjHlE47Gaj8oPkdVHDdzd9ToAkUSPIsxRx; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WWM2vn1KHS_k1aSj6DvSDWv; SINAGLOBAL=7552724259118.417.1447641174437; ULV=1447691774405:2:2:2:6434341784127.688.1447691774390:1447641174455; YF-Page-G0=7f5e11c19f51c6954c5e18e40c0b1444; _s_tentry=-; Apache=6434341784127.688.1447691774390; USRANIME=usrmdinst_29"; # Your cookies.',
'Host':'photo.weibo.com',
'User-Agent' : 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Mobile Safari/537.36',
}
url = "http://photo.weibo.com/photos/get_all?uid=2215245793&album_id=3555077064049743&count=30&page=1&type=3&__rnd=1503164588496"
r = requests.get(url,headers = headers)
pics = r.json()["data"]["photo_list"]
for x in pics :
name = x["pic_name"]
src = "http://wx1.sinaimg.cn/large/"+name
download(name,src)
def download(name,src):
try:
urlretrieve(src,'img/'+name)
print('ok!')
except Exception as e :
print(src,'download fail!')
if __name__=='__main__':
main()
Traceback (most recent call last):
File "F:/python/User/爬取新浪微博.py", line 28, in <module>
main()
File "F:/python/User/爬取新浪微博.py", line 15, in main
raise JSONDecodeError(errmsg, string, idx)
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
答案 0 :(得分:0)
如果您添加print(r.status_code, r.content)
,您会看到使用某些HTML获得404响应。这就是JSONDecodeError
失败的原因。
答案 1 :(得分:0)
print(r.status_code)=&#39; 404响应&#39;并打印(r.content)=&#39;找不到文件&#39;