图集谷-写真集-爬虫-2.1「建议收藏」
生活随笔
收集整理的這篇文章主要介紹了
图集谷-写真集-爬虫-2.1「建议收藏」
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了使爬蟲代碼更加扁平化,因此,我對2.0版本爬蟲進行了一定的修改。
話不多說,請看代碼。
# 導入庫
import os, time, requests
# 請求頭
headers = {
'User-Agent': 'Chrome 10.0'
}
# 圖片數據列表
pictures = []
# 獲取圖片數據
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)
# 創建目的文件夾
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
# 保存圖片數據
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()
# 主函數,調用函數
def main():
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()
# 主程序入口
if __name__ == '__main__':
Page_Num = input('Enter the Page_name:') # Page_Num指下文url所代指的數字
Num = int(input('Enter the Num:')) + 1 # Num為寫真集的圖片數
print('Finish!')
大家有任何疑問,都可以在下面評論區進行留言,我會不定時的對評論進行回復。
感謝大家的支持!
總結
以上是生活随笔為你收集整理的图集谷-写真集-爬虫-2.1「建议收藏」的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阴阳师丑时之女在哪打
- 下一篇: SQLITE_ERROR - table