欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的完整實(shí)例

 更新時(shí)間:2021年03月09日 09:11:57   作者:割韭菜的喵醬  
這篇文章主要給大家介紹了關(guān)于Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

動(dòng)態(tài)網(wǎng)頁(yè)爬取是爬蟲學(xué)習(xí)中的一個(gè)難點(diǎn)。本文將以知名插畫網(wǎng)站pixiv為例,簡(jiǎn)要介紹動(dòng)態(tài)網(wǎng)頁(yè)爬取的方法。

寫在前面

本代碼的功能是輸入畫師的pixiv id,下載畫師的所有插畫。由于本人水平所限,所以代碼不能實(shí)現(xiàn)自動(dòng)登錄pixiv,需要在運(yùn)行時(shí)手動(dòng)輸入網(wǎng)站的cookie值。

重點(diǎn):請(qǐng)求頭的構(gòu)造,json文件網(wǎng)址的查找,json中信息的提取

分析

創(chuàng)建文件夾

根據(jù)畫師的id創(chuàng)建文件夾(相關(guān)路徑需要自行調(diào)整)。

def makefolder(id): # 根據(jù)畫師的id創(chuàng)建對(duì)應(yīng)的文件夾
	try:
		folder = os.path.join('E:\pixivimages', id)
		os.mkdir(folder)
		return folder
	except(FileExistsError):
		print('the folder exists!')
		exit()

獲取作者所有圖片的id

訪問(wèn)url:https://pixiv.net/ajax/user/畫師id/profile/all(這個(gè)json可以在畫師主頁(yè)url:https://www.pixiv.net/users/畫師id 的開發(fā)者面板中找到,如圖:)

json內(nèi)容:

將json文檔轉(zhuǎn)化為python的字典,提取對(duì)應(yīng)元素即可獲取所有的插畫id。

def getAuthorAllPicID(id, cookie): # 獲取畫師所有圖片的id
	url = 'https://pixiv.net/ajax/user/' + id + '/profile/all' # 訪問(wèn)存有畫師所有作品
	headers = {
		'User-Agent': user_agent,
		'Cookie': cookie,
		'Referer': 'https://www.pixiv.net/artworks/' 
		# referer不能缺少,否則會(huì)403
	}
	res = requests.get(url, headers=headers, proxies=proxies)
	if res.status_code == 200:
		resdict = json.loads(res.content)['body']['illusts'] # 將json轉(zhuǎn)化為python的字典后提取元素
		return [key for key in resdict] # 返回所有圖片id
	else:
		print("Can not get the author's picture ids!")
		exit()

獲取圖片的真實(shí)url并下載

訪問(wèn)url:https://www.pixiv.net/ajax/illust/圖片id?lang=zh,可以看到儲(chǔ)存有圖片真實(shí)地址的json:(這個(gè)json可以在圖片url:https://www.pixiv.net/artworks/圖片id 的開發(fā)者面板中找到)

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

def getPictures(folder, IDlist, cookie): # 訪問(wèn)圖片儲(chǔ)存的真實(shí)網(wǎng)址
	for picid in IDlist:
		url1 = 'https://www.pixiv.net/artworks/{}'.format(picid) # 注意這里referer必不可少,否則會(huì)報(bào)403
		headers = {
			'User-Agent': user_agent,
			'Cookie': cookie,
			'Referer': url1
		}
		url = 'https://www.pixiv.net/ajax/illust/' + str(picid) + '?lang = zh' #訪問(wèn)儲(chǔ)存圖片網(wǎng)址的json
		res = requests.get(url, headers=headers, proxies=proxies)
		if res.status_code == 200:
			data = json.loads(res.content)
			picurl = data['body']['urls']['original'] # 在字典中找到儲(chǔ)存圖片的路徑與標(biāo)題
			title = data['body']['title']
			title = changeTitle(title) # 調(diào)整標(biāo)題
			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) # 如果圖片中有下列符號(hào),可能會(huì)導(dǎo)致圖片無(wú)法成功下載
	# 注意可能還會(huì)有許多不能用于文件命名的符號(hào),如果找到對(duì)應(yīng)符號(hào)要將其添加到正則表達(dá)式中
	if title == '無(wú)題': # pixiv中有許多名為'無(wú)題'(日文)的圖片,需要對(duì)它們加以區(qū)分以防止覆蓋
		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() # 生成假的瀏覽器請(qǐng)求頭,防止被封ip
user_agent = ua.random # 隨機(jī)選擇一個(gè)瀏覽器
proxies = {'http': 'http://127.0.0.1:51837', 'https': 'http://127.0.0.1:51837'} # 代理,根據(jù)自己實(shí)際情況調(diào)整,注意在請(qǐng)求時(shí)一定不要忘記代理!!


def makefolder(id): # 根據(jù)畫師的id創(chuàng)建對(duì)應(yīng)的文件夾
	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' # 訪問(wèn)存有畫師所有作品
	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轉(zhuǎn)化為python的字典后提取元素
		return [key for key in resdict] # 返回所有圖片id
	else:
		print("Can not get the author's picture ids!")
		exit()


def getPictures(folder, IDlist, cookie): # 訪問(wèn)圖片儲(chǔ)存的真實(shí)網(wǎng)址
	for picid in IDlist:
		url1 = 'https://www.pixiv.net/artworks/{}'.format(picid) # 注意這里referer必不可少,否則會(huì)報(bào)403
		headers = {
			'User-Agent': user_agent,
			'Cookie': cookie,
			'Referer': url1
		}
		url = 'https://www.pixiv.net/ajax/illust/' + str(picid) + '?lang = zh' #訪問(wèn)儲(chǔ)存圖片網(wǎng)址的json
		res = requests.get(url, headers=headers, proxies=proxies)
		if res.status_code == 200:
			data = json.loads(res.content)
			picurl = data['body']['urls']['original'] # 在字典中找到儲(chǔ)存圖片的路徑與標(biāo)題
			title = data['body']['title']
			title = changeTitle(title) # 調(diào)整標(biāo)題
			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) # 如果圖片中有下列符號(hào),可能會(huì)導(dǎo)致圖片無(wú)法成功下載
	# 注意可能還會(huì)有許多不能用于文件命名的符號(hào),如果找到對(duì)應(yīng)符號(hào)要將其添加到正則表達(dá)式中
	if title == '無(wú)題': # pixiv中有許多名為'無(wú)題'(日文)的圖片,需要對(duì)它們加以區(qū)分以防止覆蓋
		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:') # 半自動(dòng)爬蟲,需要自己事先登錄pixiv以獲取cookie
	folder = makefolder(id)
	IDlist = getAuthorAllPicID(id, cookie)
	getPictures(folder, IDlist, cookie)


if __name__ == '__main__':
	main()

效果

總結(jié)

到此這篇關(guān)于Python爬取動(dòng)態(tài)網(wǎng)頁(yè)中圖片的文章就介紹到這了,更多相關(guān)Python爬取動(dòng)態(tài)網(wǎng)頁(yè)圖片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyTorch之torch.matmul函數(shù)的使用及說(shuō)明

    PyTorch之torch.matmul函數(shù)的使用及說(shuō)明

    PyTorch的torch.matmul是一個(gè)強(qiáng)大的矩陣乘法函數(shù),支持不同維度張量的乘法運(yùn)算,包括廣播機(jī)制。提供了矩陣乘法的語(yǔ)法,參數(shù)說(shuō)明,以及使用示例,幫助理解其應(yīng)用方式和乘法規(guī)則
    2024-09-09
  • python實(shí)現(xiàn)引用其他路徑包里面的模塊

    python實(shí)現(xiàn)引用其他路徑包里面的模塊

    這篇文章主要介紹了python實(shí)現(xiàn)引用其他路徑包里面的模塊,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Python實(shí)現(xiàn)注冊(cè)登錄功能

    Python實(shí)現(xiàn)注冊(cè)登錄功能

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)注冊(cè)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python文件基本操作open函數(shù)應(yīng)用與示例詳解

    Python文件基本操作open函數(shù)應(yīng)用與示例詳解

    這篇文章主要為大家介紹了Python文件基本操作open函數(shù)應(yīng)用與示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Python之torch.no_grad()函數(shù)使用和示例

    Python之torch.no_grad()函數(shù)使用和示例

    這篇文章主要介紹了Python之torch.no_grad()函數(shù)使用和示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Python3中FuzzyWuzzy庫(kù)實(shí)例用法

    Python3中FuzzyWuzzy庫(kù)實(shí)例用法

    在本篇文章中小編給各位整理了關(guān)于Python3z中FuzzyWuzzy庫(kù)實(shí)例用法及相關(guān)代碼,有興趣的朋友們可以參考下。
    2020-11-11
  • pytorch model.cuda()花費(fèi)時(shí)間很長(zhǎng)的解決

    pytorch model.cuda()花費(fèi)時(shí)間很長(zhǎng)的解決

    這篇文章主要介紹了pytorch model.cuda()花費(fèi)時(shí)間很長(zhǎng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python編寫登陸接口的方法

    Python編寫登陸接口的方法

    這篇文章主要為大家詳細(xì)介紹了Python編寫登陸接口的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Django中信號(hào)signals的簡(jiǎn)單使用方法

    Django中信號(hào)signals的簡(jiǎn)單使用方法

    這篇文章主要給大家介紹了關(guān)于Django中信號(hào)signals的簡(jiǎn)單使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python訪問(wèn)MySQL封裝的常用類實(shí)例

    Python訪問(wèn)MySQL封裝的常用類實(shí)例

    這篇文章主要介紹了Python訪問(wèn)MySQL封裝的常用類,實(shí)例詳述了針對(duì)MySQL使用query執(zhí)行select及使用update進(jìn)行insert、delete等操作的方法,需要的朋友可以參考下
    2014-11-11

最新評(píng)論