python爬蟲實例之獲取動漫截圖
引言
之前有些無聊(呆在家里實在玩的膩了),然后就去B站看了一些python爬蟲視頻,沒有進行基礎的理論學習,也就是直接開始實戰(zhàn),感覺跟背公式一樣的進行爬蟲,也算行吧,至少還能爬一些東西,hhh。我今天來分享一個我的爬蟲代碼。
正文
話不多說,直接上完整代碼
ps:這個代碼有些問題 每次我爬到fate的圖片它就給我報錯,我只好用個try來跳過了,如果有哪位大佬能幫我找出錯誤并給與糾正,我將不勝感激
import requests as r import re import os import time file_name = "動漫截圖" if not os.path.exists(file_name): os.mkdir(file_name) for p in range(1,34): print("--------------------正在爬取第{}頁內(nèi)容------------------".format(p)) url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p) headers = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"} resp = r.get(url, headers=headers) html = resp.text images = re.findall('data-original="(.*?)" ', html) names =re.findall('title="(.*?)"', html) #print(images) #print(names) dic = dict(zip(images, names)) for image in images: time.sleep(1) print(image, dic[image]) name = dic[image] #name = image.split('/')[-1] i = r.get(image, headers=headers).content try: with open(file_name + '/' + name + '.jpg' , 'wb') as f: f.write(i) except FileNotFoundError: continue
先導入要使用的庫
import requests as r import re import os import time
然后去分析要去爬的網(wǎng)址: https://www.acgimage.com/shot/recommend
下圖是網(wǎng)址的內(nèi)容:
好了 url已經(jīng)確定
下面去尋找headers
找到user-agent 將其內(nèi)容復制到headers中
第一步就完成了
下面是代碼展示
url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p) headers = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36" }
然后檢索要爬的圖片內(nèi)容
從上圖就可以找到圖片的位置:data-origina=后面的內(nèi)容
以及圖片的名字:title=后面的內(nèi)容
然后用正則表達式re來檢索就行了
images = re.findall('data-original="(.*?)" ', html) names =re.findall('title="(.*?)"', html)
最后將其保存就好了
i = r.get(image, headers=headers).content with open(file_name + '/' + name + '.jpg' , 'wb') as f: f.write(i)
還有就是一些細節(jié)了
比如換頁
第一頁網(wǎng)址:
https://www.acgimage.com/shot/recommend
第二頁網(wǎng)址:https://www.acgimage.com/shot/recommend?page=2
然后將page后面的數(shù)字改動就可以跳到相應的頁面
換頁的問題也就解決了
or p in range(1,34): url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p)
以及將爬到的圖片放到自己建立的文件zh
使用了os庫
file_name = "動漫截圖" if not os.path.exists(file_name): os.mkdir(file_name)
以及為了不影響爬取的網(wǎng)站 使用了sleep函數(shù)
雖然爬取的速度慢了一些
但是這是應遵守的道德
time.sleep(1)
以上 這就是我的爬蟲過程
還是希望大佬能解決我的錯誤之處
萬分感謝
總結
到此這篇關于python爬蟲實例之獲取動漫截圖的文章就介紹到這了,更多相關python爬蟲獲取動漫截圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制
這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制,文章基于python的相關資料展開詳細的餅圖繪制,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表
使用Python從Excel讀取數(shù)據(jù)并在PowerPoint幻燈片中創(chuàng)建圖表不僅能夠極大地簡化圖表創(chuàng)建過程,通過Python這一橋梁,我們可以輕松實現(xiàn)數(shù)據(jù)自動化處理和圖表生成,本文將演示如何使用Python讀取Excel數(shù)據(jù)在PPT中創(chuàng)建圖表,需要的朋友可以參考下2024-08-08python利用smtplib實現(xiàn)QQ郵箱發(fā)送郵件
這篇文章主要為大家詳細介紹了python利用smtplib實現(xiàn)QQ郵箱發(fā)送郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python split() 函數(shù)拆分字符串將字符串轉化為列的方法
今天小編就為大家分享一篇Python split() 函數(shù)拆分字符串將字符串轉化為列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07