最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python爬取動態(tài)網(wǎng)頁中圖片的完整實例

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

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

寫在前面

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

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

分析

創(chuàng)建文件夾

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

def makefolder(id): # 根據(jù)畫師的id創(chuàng)建對應(yīng)的文件夾
	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 的開發(fā)者面板中找到,如圖:)

json內(nèi)容:

將json文檔轉(zhuǎn)化為python的字典,提取對應(yīng)元素即可獲取所有的插畫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轉(zhuǎn)化為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 的開發(fā)者面板中找到)

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

def getPictures(folder, IDlist, cookie): # 訪問圖片儲存的真實網(wǎng)址
	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' #訪問儲存圖片網(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'] # 在字典中找到儲存圖片的路徑與標(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) # 如果圖片中有下列符號,可能會導(dǎo)致圖片無法成功下載
	# 注意可能還會有許多不能用于文件命名的符號,如果找到對應(yīng)符號要將其添加到正則表達式中
	if title == '無題': # pixiv中有許多名為'無題'(日文)的圖片,需要對它們加以區(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() # 生成假的瀏覽器請求頭,防止被封ip
user_agent = ua.random # 隨機選擇一個瀏覽器
proxies = {'http': 'http://127.0.0.1:51837', 'https': 'http://127.0.0.1:51837'} # 代理,根據(jù)自己實際情況調(diào)整,注意在請求時一定不要忘記代理!!


def makefolder(id): # 根據(jù)畫師的id創(chuàng)建對應(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' # 訪問存有畫師所有作品
	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ǎng)址
	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' #訪問儲存圖片網(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'] # 在字典中找到儲存圖片的路徑與標(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) # 如果圖片中有下列符號,可能會導(dǎo)致圖片無法成功下載
	# 注意可能還會有許多不能用于文件命名的符號,如果找到對應(yīng)符號要將其添加到正則表達式中
	if title == '無題': # pixiv中有許多名為'無題'(日文)的圖片,需要對它們加以區(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:') # 半自動爬蟲,需要自己事先登錄pixiv以獲取cookie
	folder = makefolder(id)
	IDlist = getAuthorAllPicID(id, cookie)
	getPictures(folder, IDlist, cookie)


if __name__ == '__main__':
	main()

效果

總結(jié)

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

相關(guān)文章

  • python實現(xiàn)復(fù)制大量文件功能

    python實現(xiàn)復(fù)制大量文件功能

    這篇文章主要為大家詳細介紹了python實現(xiàn)復(fù)制大量文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • python 實現(xiàn)表情識別

    python 實現(xiàn)表情識別

    這篇文章主要介紹了python 實現(xiàn)表情識別的示例代碼,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11
  • Python Thread虛假喚醒概念與防范詳解

    Python Thread虛假喚醒概念與防范詳解

    這篇文章主要介紹了Python Thread虛假喚醒概念與防范,虛假喚醒是一種現(xiàn)象,它只會出現(xiàn)在多線程環(huán)境中,指的是在多線程環(huán)境下,多個線程等待在同一個條件上,等到條件滿足時,所有等待的線程都被喚醒,但由于多個線程執(zhí)行的順序不同
    2023-02-02
  • OpenCV圖片漫畫效果的實現(xiàn)示例

    OpenCV圖片漫畫效果的實現(xiàn)示例

    這篇文章主要介紹了OpenCV圖片漫畫效果的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Python實現(xiàn)隨機生成圖片驗證碼詳解

    Python實現(xiàn)隨機生成圖片驗證碼詳解

    這篇文章主要介紹了如何利用Python生成隨機的圖片驗證碼 并打印驗證碼的值,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起試試
    2022-01-01
  • python 通過SSHTunnelForwarder隧道連接redis的方法

    python 通過SSHTunnelForwarder隧道連接redis的方法

    今天小編就為大家分享一篇python 通過SSHTunnelForwarder隧道連接redis的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python實現(xiàn)線性判別分析(LDA)的MATLAB方式

    Python實現(xiàn)線性判別分析(LDA)的MATLAB方式

    今天小編大家分享一篇Python實現(xiàn)線性判別分析(LDA)的MATLAB方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python中pathlib 面向?qū)ο蟮奈募到y(tǒng)路徑

    python中pathlib 面向?qū)ο蟮奈募到y(tǒng)路徑

    文章介紹了Python 3.4+內(nèi)置的標(biāo)準庫pathlib,該庫提供面向?qū)ο蟮奈募到y(tǒng)路徑處理,通過pathlib,可以更方便地進行路徑操作,感興趣的朋友一起看看吧
    2024-12-12
  • 如何利用Python獲取鼠標(biāo)的實時位置

    如何利用Python獲取鼠標(biāo)的實時位置

    這篇文章主要給大家介紹了關(guān)于如何利用Python獲取鼠標(biāo)的實時位置的相關(guān)資料,主要利用的是pyautogui,一個自動化鍵鼠操作的Python類庫,需要的朋友可以參考下
    2022-01-01
  • python生成詞云的實現(xiàn)方法(推薦)

    python生成詞云的實現(xiàn)方法(推薦)

    下面小編就為大家?guī)硪黄猵ython生成詞云的實現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論

鹿泉市| 宝鸡市| 方城县| 治多县| 五华县| 南平市| 林周县| 大庆市| 凤庆县| 长阳| 扎兰屯市| 中牟县| 马鞍山市| 拜城县| 连平县| 无锡市| 昭平县| 南城县| 西畴县| 永吉县| 五大连池市| 上蔡县| 岚皋县| 津市市| 吉木乃县| 文水县| 紫阳县| 醴陵市| 红河县| 永嘉县| 安庆市| 泸西县| 临清市| 大新县| 肥东县| 石柱| 黑河市| 张家口市| 泰顺县| 太康县| 斗六市|