练习---爬取堆糖上的头像

import requests
import json
import os


def save_photo(num,name):
	nums=0
	count=0
	while nums<=30:
		url="https://www.duitang.com/napi/blog/list/by_filter_id/"
		params={
		'include_fields':'top_comments,is_root,source_link,item,buyable,root_id,status,like_count,sender,album,reply_count',
		'filter_id':name,
		'start':nums,
		'_':'1561373748107'
		}
		res=requests.get(url,params=params)
		js=res.json()
		pics=js['data']['object_list']
		
		for i in range(len(pics)):
			pic_url=pics[i]['photo']['path']
			r=requests.get(pic_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\\"+str(num+1)+".jpg","wb+") as f:
				f.write(r.content)
				
			num+=1
			count+=1
			if count>29:
				break
		nums+=24	
	return num
num=0
q='y'

while q=='y':
	name=input("头像分类:\n1.女生\n2.男生\n3.情侣\n4.欧美\n5.文字\n6.个性\n7.卡通\n请输入你想下载的头像分类编号:")
	if name=='1':
		num=save_photo(num,"头像_女生")
		q=input("输入y继续,否则退出:")
	elif name=='2':
		num=save_photo(num,"头像_男生")
		q=input("输入y继续,否则退出:")
	elif name=='3':
		num=save_photo(num,"头像_情侣")
		q=input("输入y继续,否则退出:")
	elif name=='4':
		num=save_photo(num,"头像_欧美")
		q=input("输入y继续,否则退出:")
	elif name=='5':
		num=save_photo(num,"头像_文字")
		q=input("输入y继续,否则退出:")
	elif name=='6':
		num=save_photo(num,"头像_个性")
		q=input("输入y继续,否则退出:")
	elif name=='7':
		num=save_photo(num,"头像_卡通")
		q=input("输入y继续,否则退出:")
	else:
		input("输入序号错误!")
		q=input("输入y继续,否则退出:")

爬取堆糖上的头像---目前设置的每次只下载30张

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