python爬虫爬取wallpapers最新壁纸
生活随笔
收集整理的這篇文章主要介紹了
python爬虫爬取wallpapers最新壁纸
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python爬蟲爬取wallpapers最新壁紙
詳細教程請訪問:https://www.bilibili.com/video/av58978561/
詳細教程請訪問:https://www.bilibili.com/video/av58978561/
import requests #這個包里有打開網頁的包 from bs4 import BeautifulSoup #這個包里有html解析器 def getimg(url,path,name):#將步驟3封裝成函數 參數(圖片網址,路徑,名字)with requests.get(url) as resp:#打開圖片with open(path+'/'+name+'.jpg','wb') as f:#打開路徑,準備寫入圖片f.write(resp.content)#將圖片以傳進來的名字以wb(w:寫入 b:二進制方式)寫入路徑def getdurl(url,l,w):#步驟2 參數(每一頁的網址,圖片長度,圖片寬度)with requests.get(url) as resp:#打開網址html=resp.text#獲取html代碼soup=BeautifulSoup(html,'html.parser')#將代碼用bs4進行解析h1=soup.select('h1')#找到html代碼中標簽為h1(html中h1代表標題)的賦值給變量h1 這樣我們就找到了所有圖片的名字,但是還有一個網頁的標題Latest Wallpapersfor i in h1:#for循環if i.text=='Latest Wallpapers':continue#去掉網頁標題n=i.textimgurl='http://wallpaperswide.com/download/'+n.replace(' ','_').lower()+'-'+str(l)+'x'+str(w)#拼接網址,將名字中空格換成下劃線,所有字母換小寫# print(imgurl)print('正在下載'+n+'。。。')#提示信息getimg(imgurl,'D:\編程\python\wallpapers',n)#調用步驟三函數循環下載print(n+'下載完成。。。')#提示信息# url='http://wallpaperswide.com/download/Cyberpunk2077-800x480'if __name__=='__main__':#此程序作為主程序運行for i in range(2):#外循環,從第一頁循環至第十頁,這里的10可以改成別的getdurl('http://wallpaperswide.com/latest_wallpapers/page/'+str(i+1)+'.html',1366,768)#拼接網址調用步驟二函數總結
以上是生活随笔為你收集整理的python爬虫爬取wallpapers最新壁纸的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RPG Maker MV 踩坑二 新仙剑
- 下一篇: Elasticsearch配置ik中文分