好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Python爬取動態網頁中圖片的完整實例

Python爬取動態網頁中圖片的完整實例

熱門標簽:賓館能在百度地圖標注嗎 鄭州智能外呼系統中心 400電話 申請 條件 汕頭電商外呼系統供應商 crm電銷機器人 北京外呼電銷機器人招商 電銷機器人 金倫通信 云南地圖標注 南京crm外呼系統排名

動態網頁爬取是爬蟲學習中的一個難點。本文將以知名插畫網站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()

效果

總結

到此這篇關于Python爬取動態網頁中圖片的文章就介紹到這了,更多相關Python爬取動態網頁圖片內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python使用爬蟲抓取美女圖片并保存到本地的方法【測試可用】
  • 使用Python的Scrapy框架十分鐘爬取美女圖
  • Python制作爬蟲抓取美女圖
  • python實現爬蟲下載美女圖片
  • python爬蟲入門教程之點點美女圖片爬蟲代碼分享
  • python小技巧之批量抓取美女圖片
  • Python爬蟲之教你利用Scrapy爬取圖片
  • python制作微博圖片爬取工具
  • Python使用xpath實現圖片爬取
  • 只用50行Python代碼爬取網絡美女高清圖片

標簽:浙江 石家莊 梅州 懷化 文山 錫林郭勒盟 昆明 西寧

巨人網絡通訊聲明:本文標題《Python爬取動態網頁中圖片的完整實例》,本文關鍵詞  Python,爬取,動態,網頁,中,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python爬取動態網頁中圖片的完整實例》相關的同類信息!
  • 本頁收集關于Python爬取動態網頁中圖片的完整實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 美女脱??露出??吃我的??| 亚洲AV秘?无码白峰美诗| 日夜男女| 明星三级国产免费播放| 911国产这些观看| www.丝瓜成人app| 性感美女网站| 欧美成人免费在线| 中文字幕乱码中文字幕乱码| 成人??免费视频A片视频88p| 总有情人为我自相残杀| 乱Lun合集全文阅读| 国产精品一区二三区好的精华液| 久久中字| 快穿女配校园啪啪h| 国产黄色a| 免费观看男人免费桶女人视频| 春雨直播全婐app免费| 男人扒开添女人下部有好处吗| 我和两个女同事玩3p| 丰满诱人的继牳5伦韩国电影| 昭和乡下强行侵犯美人妻| 欧美午夜精品理论片A级| japanese日本护士18一19| 乳肉h女主荡| 毛茸茸xx毛茸茸| 99综合视频| 被捆私密憋尿处被棉花堵故事| 亚洲精品少妇久久久久久海角社区| 久久精品亚洲视频| 秋霞韩国理伦电影在线手机| 日韩性生活片| 极品美女?开粉嫩小泬网站 | 色哟哟?入口国产精品| 婷婷人人爽人人爽人人片| 色欲AV精品亚洲网站| 寡妇高潮一级毛片免费看软件下载| 新毛片基地| 亚洲精品一区二区三区四区高清| 国产精品一区二区午夜嘿嘿嘿小说 | 色情AV无码一级在线观看|