data_json = data_json['data'] NameError: name 'data_json' is not defined
时间: 2024-04-22 08:26:35 浏览: 188
data.json
抱歉,我之前的回答有误。错误是因为`data_json`变量没有在代码中定义。请将以下代码块中的`data_json`变量定义并赋值为API返回的JSON数据:
```python
import requests
import os
get_url = 'https://www.dbbqb.com/api/search/json?'
params = {
'size': 100
}
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
}
response = requests.get(url=get_url, params=params, headers=headers)
data_json = response.json()
data_json = data_json['data']
pic_paths = []
for detail_data in data_json:
pic_path = detail_data.get('path')
pic_paths.append(pic_path)
if not os.path.exists('./表情包'):
os.makedirs('./表情包')
for pic_url in pic_paths:
url = 'https://image.dbbqb.com/' + pic_url
pic_name = pic_url.split('/')[-1]
response = requests.get(url=url, headers=headers).content
with open(f'./表情包/{pic_name}.jpg', 'wb') as f:
f.write(response)
print(f'{pic_name}.jpg下载完成')
```
请确保您已经安装了`requests`库,并且网络连接正常。如果问题仍然存在,请提供错误信息以便我更好地帮助您解决问题。
阅读全文