使用Python爬取小姐姐圖片(beautifulsoup法)
Python有許多強大的庫用于爬蟲,如beautifulsoup、requests等,本文將以網(wǎng)站https://www.xiurenji.cc/XiuRen/為例(慎點!!),講解網(wǎng)絡爬取圖片的一般步驟。為什么選擇這個網(wǎng)站?其實與網(wǎng)站的內(nèi)容無關(guān)。主要有兩項技術(shù)層面的原因:①該網(wǎng)站的頁面構(gòu)造較有規(guī)律,適合新手對爬蟲的技巧加強認識。②該網(wǎng)站沒有反爬蟲機制,可以放心使用爬蟲。
第三方庫需求
beautifulsoup requests 步驟打開網(wǎng)站,點擊不同的頁面:發(fā)現(xiàn)其首頁是https://www.xiurenji.cc/XiuRen/,而第二頁是https://www.xiurenji.cc/XiuRen/index2.html,第三頁第四頁以此類推。為了爬蟲代碼的普適性,我們不妨從第二頁以后進行構(gòu)造url。
選中封面圖片,點擊檢查:
可以發(fā)現(xiàn),圖片的信息,都在’div’,class_=’dan’里,而鏈接又在a標簽下的href里。據(jù)此我們可以寫一段代碼提取出每一個封面圖片的url:
def getFirstPage(page): url=’https://www.xiurenji.cc/XiuRen/index’+str(page)+’.html’#獲得網(wǎng)站每一個首頁的網(wǎng)址 res=requests.get(url)#發(fā)送請求 res.encoding='gbk'#設置編碼方式為gbk html=res.text soup=BeautifulSoup(html,features=’lxml’) lists=soup.find_all(’div’,class_=’dan’)#找到儲存每一個封面圖片的標簽值 urls=[] for item in lists: url1=item.find(’a’).get(’href’)#尋找每一個封面對應的網(wǎng)址 urls.append(’https://www.xiurenji.cc’+url1)#在列表的尾部添加一個元素,達到擴充列表的目的,注意要把網(wǎng)址擴充完整 return urls#返回該主頁每一個封面對應的網(wǎng)址
點擊封面圖片,打開不同的頁面,可以發(fā)現(xiàn),首頁的網(wǎng)址是https://www.xiurenji.cc/XiuRen/xxxx.html,而第二頁的網(wǎng)址是https://www.xiurenji.cc/XiuRen/xxxx_1.html,第三第四頁同理。同樣為了普適性,我們從第二頁開始爬取。
右鍵,點擊“檢查”:
可以發(fā)現(xiàn)所有的圖片信息都儲存在’div’,class_=’img’中,鏈接、標題分別在img標簽中的src和alt中,我們同樣也可以將它們提取出來。
def getFirstPage(page): url=’https://www.xiurenji.cc/XiuRen/index’+str(page)+’.html’#獲得網(wǎng)站每一個首頁的網(wǎng)址 res=requests.get(url)#發(fā)送請求 res.encoding='gbk'#設置編碼方式為gbk html=res.text soup=BeautifulSoup(html,features=’lxml’) lists=soup.find_all(’div’,class_=’dan’)#找到儲存每一個封面圖片的標簽值 urls=[] for item in lists: url1=item.find(’a’).get(’href’)#尋找每一個封面對應的網(wǎng)址 urls.append(’https://www.xiurenji.cc’+url1)#在列表的尾部添加一個元素,達到擴充列表的目的,注意要把網(wǎng)址擴充完整 return urls#返回該主頁每一個封面對應的網(wǎng)址
完整代碼
import requestsfrom bs4 import BeautifulSoupdef getFirstPage(page): url=’https://www.xiurenji.cc/XiuRen/index’+str(page)+’.html’#獲得網(wǎng)站每一個首頁的網(wǎng)址 res=requests.get(url)#發(fā)送請求 res.encoding='gbk'#設置編碼方式為gbk html=res.text soup=BeautifulSoup(html,features=’lxml’) lists=soup.find_all(’div’,class_=’dan’)#找到儲存每一個封面圖片的標簽值 urls=[] for item in lists: url1=item.find(’a’).get(’href’)#尋找每一個封面對應的網(wǎng)址 urls.append(’https://www.xiurenji.cc’+url1)#在列表的尾部添加一個元素,達到擴充列表的目的,注意要把網(wǎng)址擴充完整 return urls#返回該主頁每一個封面對應的網(wǎng)址def download(urls): for url1 in urls: print('prepare to download pictures in '+url1) getEveryPage(url1)#下載頁面內(nèi)的圖片 print('all pictures in '+url1+'are downloaded') def getEveryPage(url1): total=0#total的作用:對屬于每一個封面內(nèi)的圖片一次編號 for n in range (1,11):#每一個封面對應下載10張圖,可自行調(diào)整 temp=url1.replace(’.html’,’’) url2=temp+’_’+str(n)+’.html’#獲得每一內(nèi)部頁面的網(wǎng)址 res=requests.get(url2) res.encoding='gbk' html=res.text soup=BeautifulSoup(html,features=’lxml’) lists1=soup.find_all(’div’,class_=’img’)#儲存圖片的路徑 for item in lists1: url=item.find(’img’).get(’src’) title=item.find(’img’).get(’alt’)#獲取圖片及其標題 picurl=’https://www.xiurenji.cc’+url#獲取完整的圖片標題 picture=requests.get(picurl).content#下載圖片 address=’D:pythonimages’+’’#自定義保存圖片的路徑 with open(address+title+str(total)+’.jpg’,’wb’) as file:#保存圖片 print('downloading'+title+str(total)) total=total+1 file.write(picture) if __name__ == '__main__': page=int(input(’input the page you want:’)) urls=getFirstPage(page) download(urls)
本文僅供學習參考,切勿用作其他用途!
到此這篇關(guān)于Python爬取小姐姐圖片(beautifulsoup法)的文章就介紹到這了,更多相關(guān)Python爬取小姐姐圖片內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php使用正則驗證密碼字段的復雜強度原理詳細講解 原創(chuàng)2. asp.net core 認證和授權(quán)實例詳解3. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)4. XML在語音合成中的應用5. ASP.NET MVC使用Boostrap實現(xiàn)產(chǎn)品展示、查詢、排序、分頁6. 基于PHP做個圖片防盜鏈7. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)8. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字9. jscript與vbscript 操作XML元素屬性的代碼10. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)
