当前位置 博文首页 > Python爬取动态网页中图片的完整实例

    Python爬取动态网页中图片的完整实例

    作者:割韭菜的喵酱 时间:2021-07-16 17:47

    动态网页爬取是爬虫学习中的一个难点。本文将以知名插画网站pixiv为例,简要介绍动态网页爬取的方法。

    写在前面

    本代码的功能是输入画师的pixiv id,下载画师的所有插画。由于本人水平所限,所以代码不能实现自动登录pixiv,需要在运行时手动输入网站的cookie值。

    重点:请求头的构造,json文件网址的查找,json中信息的提取

    分析

    创建文件夹

    根据画师的id创建文件夹(相关路径需要自行调整)。

    def makefolder(id): # 根据画师的id创建对应的文件夹
    	try:
    		folder = os.path.join('E:\pixivimages', id)
    		os.mkdir(folder)
    		return folder
    	except(FileExistsError):
    		print('the folder exists!')
    		exit()
    

    获取作者所有图片的id

    访问url:https://pixiv.net/ajax/user/画师id/profile/all(这个json可以在画师主页url:https://www.pixiv.net/users/画师id 的开发者面板中找到,如图:)

    json内容:

    将json文档转化为python的字典,提取对应元素即可获取所有的插画id。

    def getAuthorAllPicID(id, cookie): # 获取画师所有图片的id
    	url = 'https://pixiv.net/ajax/user/' + id + '/profile/all' # 访问存有画师所有作品
    	headers = {
    		'User-Agent': user_agent,
    		'Cookie': cookie,
    		'Referer': 'https://www.pixiv.net/artworks/' 
    		# referer不能缺少,否则会403
    	}
    	res = requests.get(url, headers=headers, proxies=proxies)
    	if res.status_code == 200:
    		resdict = json.loads(res.content)['body']['illusts'] # 将json转化为python的字典后提取元素
    		return [key for key in resdict] # 返回所有图片id
    	else:
    		print("Can not get the author's picture ids!")
    		exit()
    

    获取图片的真实url并下载

    访问url:https://www.pixiv.net/ajax/illust/图片id?lang=zh,可以看到储存有图片真实地址的json:(这个json可以在图片url:https://www.pixiv.net/artworks/图片id 的开发者面板中找到)

    用同样的方法提取json中有用的元素:

    def getPictures(folder, IDlist, cookie): # 访问图片储存的真实网址
    	for picid in IDlist:
    		url1 = 'https://www.pixiv.net/artworks/{}'.format(picid) # 注意这里referer必不可少,否则会报403
    		headers = {
    			'User-Agent': user_agent,
    			'Cookie': cookie,
    			'Referer': url1
    		}
    		url = 'https://www.pixiv.net/ajax/illust/' + str(picid) + '?lang = zh' #访问储存图片网址的json
    		res = requests.get(url, headers=headers, proxies=proxies)
    		if res.status_code == 200:
    			data = json.loads(res.content)
    			picurl = data['body']['urls']['original'] # 在字典中找到储存图片的路径与标题
    			title = data['body']['title']
    			title = changeTitle(title) # 调整标题
    			print(title)
    			print(picurl)
    			download(folder, picurl, title, headers)
    		else:
    			print("Can not get the urls of the pictures!")
    			exit()
    
    
    def changeTitle(title): # 为了防止
    	global i
    	title = re.sub('[*:]', "", title) # 如果图片中有下列符号,可能会导致图片无法成功下载
    	# 注意可能还会有许多不能用于文件命名的符号,如果找到对应符号要将其添加到正则表达式中
    	if title == '無題': # pixiv中有许多名为'無題'(日文)的图片,需要对它们加以区分以防止覆盖
    		title = title + str(i)
    		i = i + 1
    	return title
    
    
    def download(folder, picurl, title, headers): # 将图片下载到文件夹中
    	img = requests.get(picurl, headers=headers, proxies=proxies)
    	if img.status_code == 200:
    		with open(folder + '\\' + title + '.jpg', 'wb') as file: # 保存图片
    			print("downloading:" + title)
    			file.write(img.content)
    	else:
    		print("download pictures error!")
    

    完整代码

    import requests
    from fake_useragent import UserAgent
    import json
    import re
    import os
    
    global i
    i = 0
    ua = UserAgent() # 生成假的浏览器请求头,防止被封ip
    user_agent = ua.random # 随机选择一个浏览器
    proxies = {'http': 'http://127.0.0.1:51837', 'https': 'http://127.0.0.1:51837'} # 代理,根据自己实际情况调整,注意在请求时一定不要忘记代理!!
    
    
    def makefolder(id): # 根据画师的id创建对应的文件夹
    	try:
    		folder = os.path.join('E:\pixivimages', id)
    		os.mkdir(folder)
    		return folder
    	except(FileExistsError):
    		print('the folder exists!')
    		exit()
    
    
    def getAuthorAllPicID(id, cookie): # 获取画师所有图片的id
    	url = 'https://pixiv.net/ajax/user/' + id + '/profile/all' # 访问存有画师所有作品
    	headers = {
    		'User-Agent': user_agent,
    		'Cookie': cookie,
    		'Referer': 'https://www.pixiv.net/artworks/' 
    	}
    	res = requests.get(url, headers=headers, proxies=proxies)
    	if res.status_code == 200:
    		resdict = json.loads(res.content)['body']['illusts'] # 将json转化为python的字典后提取元素
    		return [key for key in resdict] # 返回所有图片id
    	else:
    		print("Can not get the author's picture ids!")
    		exit()
    
    
    def getPictures(folder, IDlist, cookie): # 访问图片储存的真实网址
    	for picid in IDlist:
    		url1 = 'https://www.pixiv.net/artworks/{}'.format(picid) # 注意这里referer必不可少,否则会报403
    		headers = {
    			'User-Agent': user_agent,
    			'Cookie': cookie,
    			'Referer': url1
    		}
    		url = 'https://www.pixiv.net/ajax/illust/' + str(picid) + '?lang = zh' #访问储存图片网址的json
    		res = requests.get(url, headers=headers, proxies=proxies)
    		if res.status_code == 200:
    			data = json.loads(res.content)
    			picurl = data['body']['urls']['original'] # 在字典中找到储存图片的路径与标题
    			title = data['body']['title']
    			title = changeTitle(title) # 调整标题
    			print(title)
    			print(picurl)
    			download(folder, picurl, title, headers)
    		else:
    			print("Can not get the urls of the pictures!")
    			exit()
    
    
    def changeTitle(title): # 为了防止
    	global i
    	title = re.sub('[*:]', "", title) # 如果图片中有下列符号,可能会导致图片无法成功下载
    	# 注意可能还会有许多不能用于文件命名的符号,如果找到对应符号要将其添加到正则表达式中
    	if title == '無題': # pixiv中有许多名为'無題'(日文)的图片,需要对它们加以区分以防止覆盖
    		title = title + str(i)
    		i = i + 1
    	return title
    
    
    def download(folder, picurl, title, headers): # 将图片下载到文件夹中
    	img = requests.get(picurl, headers=headers, proxies=proxies)
    	if img.status_code == 200:
    		with open(folder + '\\' + title + '.jpg', 'wb') as file: # 保存图片
    			print("downloading:" + title)
    			file.write(img.content)
    	else:
    		print("download pictures error!")
    
    
    def main():
    	global i
    	id = input('input the id of the artist:')
    	cookie = input('input your cookie:') # 半自动爬虫,需要自己事先登录pixiv以获取cookie
    	folder = makefolder(id)
    	IDlist = getAuthorAllPicID(id, cookie)
    	getPictures(folder, IDlist, cookie)
    
    
    if __name__ == '__main__':
    	main()
    
    

    效果

    总结

    jsjbwy
    下一篇:没有了