愛美之心人皆有之,嘻嘻,小編每天都坐在電腦前,除了看小姐姐照片時是最賞心悅目心情愉悅的,直接開始吧!!
Python有許多強大的庫用于爬蟲,如beautifulsoup、requests等,本文將以網站https://www.xiurenji.cc/XiuRen/為例,講解網路爬取圖片的一般步驟,
為什么選擇這個網站?其實與網站的內容無關,主要有兩項技術層面的原因:①該網站的頁面構造較有規律,適合新手對爬蟲的技巧加強認識,②該網站沒有反爬蟲機制,可以放心使用爬蟲,

第三方庫需求
- beautifulsoup
- requests
步驟
打開網站,點擊不同的頁面:
發現其首頁是https://www.xiurenji.cc/XiuRen/,而第二頁是https://www.xiurenji.cc/XiuRen/index2.html,第三頁第四頁以此類推,為了爬蟲代碼的普適性,我們不妨從第二頁以后進行構造url,

選中封面圖片,點擊檢查:

可以發現,圖片的資訊,都在’div’,class_='dan’里,而鏈接又在a標簽下的href里,據此我們可以寫一段代碼提取出每一個封面圖片的url:
def getFirstPage(page):
url='https://www.xiurenji.cc/XiuRen/index'+str(page)+'.html'#獲得網站每一個首頁的網址
res=requests.get(url)#發送請求
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')#尋找每一個封面對應的網址
下面公眾號里面有更多免費有關Python的學習資料

urls.append(‘https://www.xiurenji.cc’+url1)#在串列的尾部添加一個元素,達到擴充串列的目的,注意要把網址擴充完整
return urls#回傳該主頁每一個封面對應的網址
點擊封面圖片,打開不同的頁面,可以發現,首頁的網址是https://www.xiurenji.cc/XiuRen/xxxx.html,而第二頁的網址是https://www.xiurenji.cc/XiuRen/xxxx_1.html,第三第四頁同理,同樣為了普適性,我們從第二頁開始爬取,

右鍵,點擊“檢查”:

可以發現所有的圖片資訊都儲存在’div’,class_='img’中,鏈接、標題分別在img標簽中的src和alt中,我們同樣也可以將它們提取出來,
def getFirstPage(page):
url='https://www.xiurenji.cc/XiuRen/index'+str(page)+'.html'#獲得網站每一個首頁的網址
res=requests.get(url)#發送請求
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')#尋找每一個封面對應的網址
urls.append('https://www.xiurenji.cc'+url1)#在串列的尾部添加一個元素,達到擴充串列的目的,注意要把網址擴充完整
return urls#回傳該主頁每一個封面對應的網址
完整代碼如下
import requests
from bs4 import BeautifulSoup
def getFirstPage(page):
url='https://www.xiurenji.cc/XiuRen/index'+str(page)+'.html'#獲得網站每一個首頁的網址
res=requests.get(url)#發送請求
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')#尋找每一個封面對應的網址
urls.append('https://www.xiurenji.cc'+url1)#在串列的尾部添加一個元素,達到擴充串列的目的,注意要把網址擴充完整
return urls#回傳該主頁每一個封面對應的網址
def download(urls):
for url1 in urls:
print("prepare to download pictures in "+url1)
getEveryPage(url1)#下載頁面內的圖片
print("all pictures in "+url1+"are downloaded")
def getEveryPage(url1):
total=0#total的作用:對屬于每一個封面內的圖片一次編號
for n in range (1,11):#每一個封面對應下載10張圖,可自行調整
temp=url1.replace('.html','')
url2=temp+'_'+str(n)+'.html'#獲得每一內部頁面的網址
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)

本文僅提供學習參考啊~這篇文章就介紹到這了,想學習更多有關Python的內容就看小編的主頁私聊我,或關注上方公眾號,獲取更多免費資源,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/281643.html
標籤:python
下一篇:python基礎知識(持續更新)
