练习---爬取堆糖上搜索的图片,并下载下来

import requests
import json
import os

def search_photo(name,max_n,count,num):
	
	url="https://www.duitang.com/napi/blog/list/by_search/"
	params={
	'kw':name,
	'type':'feed',
	'include_fields':'top_comments,is_root,source_link,item,buyable,root_id,status,like_count,like_id,sender,album,reply_count,favorite_blog_id',
	'_type':'', 
	'start':count,
	'_':'1561448504576'}
	res=requests.get(url,params=params)
	js=res.json()
	pics=js['data']['object_list']
	for i in range(len(pics)):
		photo_url=pics[i]['photo']['path']
		print(photo_url)
		photo_res=requests.get(photo_url)
		if not os.path.exists("C:\\Users\\Xpeng\\Desktop\\pics\\"):
			os.makedirs("C:\\Users\\Xpeng\\Desktop\\pics\\")
		with open("C:\\Users\\Xpeng\\Desktop\\pics\\"+photo_url.split('/')[7],'wb+') as f:
			f.write(photo_res.content)
		num+=1
		if int(num+1)>int(max_n):
			break
	return num

num=0
name=input("输入你想搜索的图片:")
max_n=input("你想下载多少张:")
count=0
while count

 

你可能感兴趣的:(爬虫学习)