图集谷-写真集-爬虫-2.0
生活随笔
收集整理的這篇文章主要介紹了
图集谷-写真集-爬虫-2.0
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
經(jīng)過(guò),對(duì)上一版爬蟲(chóng)的仔細(xì)研究,我換了一個(gè)編寫思路,寫了這個(gè)2.0版本。
依然廢話不多說(shuō),直接上代碼。
# 導(dǎo)入庫(kù)
import os, time, requests
# 請(qǐng)求頭
headers = {
'User-Agent': 'Chrome 10.0'
}
# 圖片數(shù)據(jù)列表
pictures = []
# 獲取圖片數(shù)據(jù)
def get_pic(url, nums):
for num in range(0, int(nums)):
url_all = str(url) + f'{str(num)}.jpg'
res = requests.get(url=url_all, headers=headers)
pic_content = res.content
pictures.append(pic_content)
time.sleep(0.5)
# 創(chuàng)建目的文件夾
def get_path(path_name):
if not os.path.exists(f'C:/Users/liu/Desktop/圖集/{path_name}'):
os.mkdir(f'C:/Users/liu/Desktop/圖集/{path_name}')
else:
pass
# 保存圖片數(shù)據(jù)
def save_data():
for pic,i in zip(pictures, range(int(Num))):
picture = open(f'C:/Users/liu/Desktop/圖集/{str(Page_Num)}/{str(i)}.jpg', 'wb')
picture.write(pic)
picture.close()
# 主程序入口
if __name__ == '__main__':
Page_Num = input('Enter the Page_name:') # Page_Num指下文url所代指的數(shù)字
Num = int(input('Enter the Num:')) + 1 # Num為寫真集的圖片數(shù)
url = f'https://tjg.gzhuibei.com/a/1/{str(Page_Num)}/' #
get_path(path_name=Page_Num)
get_pic(url=url, nums=Num)
save_data()
print('Finish!')
在編寫的第一版爬蟲(chóng)的過(guò)程中,我發(fā)現(xiàn)可以通過(guò)算從而確定每張圖片的url。因此,在第二版中,我省去了對(duì)lxml庫(kù)的運(yùn)用。
通過(guò)運(yùn)算得到所有圖片的url,相對(duì)于第一版更簡(jiǎn)便,也不用更多地訪問(wèn),節(jié)省了下載時(shí)間。
在這個(gè)圖集谷爬蟲(chóng)-2.0完成后,如果能夠的話,我會(huì)繼續(xù)對(duì)圖集谷爬蟲(chóng)的不斷優(yōu)化。
感謝大家的支持!
總結(jié)
以上是生活随笔為你收集整理的图集谷-写真集-爬虫-2.0的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用 cx recipe 安装 Hybr
- 下一篇: 【SQRT】函数使用技巧