问题遇到的现象和发生背景
Python爬虫系列(二)——Python爬虫批量下载百度图片
问题相关代码,请勿粘贴截图
list_image_link.append(jsonInfo['data'][index]['thumbURL'])
运行结果及报错内容
没搞懂这行代码什么意思,以及thumbURL是什么意思,中间的index是什么意思
望解答 谢谢
Python爬虫系列(二)——Python爬虫批量下载百度图片
list_image_link.append(jsonInfo['data'][index]['thumbURL'])
没搞懂这行代码什么意思,以及thumbURL是什么意思,中间的index是什么意思
望解答 谢谢
收起
这个是我写的文章,这句程序之前是在百度上获取到了相关数据的json文件,名称为jsonInfo的变量就表示这个json格式的数据,json数据格式是“key-value”,也就是可以通过key去获取value。在jsonInfo中,要获取的数据保存在了key为“data”的数据中,jsonInfo['data']就表示取出key为“data”的数据,取到的数据是一个长度为30的一个数组,因此可以通过遍历将数组的每一项取出来,这个index就是就是取数组数据的下标索引。取到的每一项数据也遵循给json格式,关键的信息,就是目标图像的链接保存在key为“thumbURL”所对应的value中,类似于jsonInfo['data'],在jsonInfo['data'][index]后加上['thumbURL']就表示取出数组中每一项key为thumbURL的数据,也就是真实的图像链接。取出来之后,我再将每个链接保存在事先声明的数组里,等待后续使用。
总结来说:
你可以使用下面的代码输出一下。
import requests # 导入requests包
import json
url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&logid=6991546938775238432&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%90%91%E6%97%A5%E8%91%B5&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=&z=&ic=&hd=&latest=©right=&word=%E5%90%91%E6%97%A5%E8%91%B5&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&nojc=&pn=30&rn=30&gsm=1e&1635046467636='
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.30'}
strhtml = requests.get(url, headers=headers) # Get方式获取网页数据
jsonInfo = json.loads(strhtml.text)
for index in range(30):
print(jsonInfo['data'][index]['thumbURL'])
报告相同问题?