爬虫返回的Json数据怎么玩?一篇文章搞定!
2、Json长啥样
{
"images": [
{
"src": "https://img1.doubanio.com/view/photo/photo/public/p2185662697.webp",
"author": "蹦咕噜霸会念经",
"url": "https://www.douban.com/link2/?url=http%3A%2F%2Fwww.douban.com%2Fphotos%2Fphoto%2F2185662697%2F&query=%E5%AE%8B%E6%99%BA%E5%AD%9D&cat_id=1025&type=search",
"id": "2185662697",
"title": "有一种美叫做宋智孝",
"width": 381,
"height": 500
},
{
"src": "https://img1.doubanio.com/view/photo/photo/public/p2185662777.webp",
"author": "蹦咕噜霸会念经",
"url": "https://www.douban.com/link2/?url=http%3A%2F%2Fwww.douban.com%2Fphotos%2Fphoto%2F2185662777%2F&query=%E5%AE%8B%E6%99%BA%E5%AD%9D&cat_id=1025&type=search",
"id": "2185662777",
"title": "有一种美叫做宋智孝",
"width": 452,
"height": 600
}
],
"total": 2658,
"limit": 20,
"more": true
}
3、在Python中玩转Json
json.dumps() 将Python对象(字符串)转换成为Json对象(字典)
json.loads() 将Json对象转换成为Python对象
4、实战演练
import requests
import pandas as pd
query = '宋智孝'
headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"}
url = 'https://www.douban.com/j/search_photo?q=' + query
response = requests.get(url, headers=headers).json() # 得到返回结果
# print(response)
result = []
for image in response['images']:
dict = {}
dict["pic"] = image["src"]
dict["author"] = image["author"]
dict["title"] = image["title"]
result.append(dict)
print(image['src']) # 查看当前下载的图片网址
download(image['src'], image['id']) # 下载一张图片
result_pd = pd.DataFrame(result)
result_pd.to_csv("result_pd.csv",encoding='utf_8_sig')
def download(src, id):
dir = './' + str(id) + '.jpg'
try:
pic = requests.get(src, headers=headers, timeout=10)
# print(pic)
with open(dir, "wb") as f:
f.write(pic.content)
except requests.exceptions.ConnectionError:
print('图片无法下载')
5、我是小结
-END-
已有50万人
领取Python案例实战课世界正在奖励坚持学习的人!