python爬图片教程_python爬去妹子网整个图片资源教程(最详细版)
爬取妹子網(wǎng)的低級教程連接如下:[爬妹子網(wǎng)](https://blog.csdn.net/baidu_35085676/article/details/68958267)
ps:只支持單個(gè)套圖下載,不支持整體下載
在說說我的這個(gè)爬蟲代碼的設(shè)計(jì)思路:
①當(dāng)我們?yōu)g覽這個(gè)網(wǎng)站時(shí),會(huì)發(fā)現(xiàn),每一個(gè)頁面的URL都是以網(wǎng)站的域名+page+頁數(shù)組成,這樣我們就可以逐一的訪問該網(wǎng)站的網(wǎng)頁了
②當(dāng)我們看圖片列表時(shí)中,把鼠標(biāo)放到圖片,右擊檢查,我們發(fā)現(xiàn),圖片的內(nèi)容由ul包裹的li組成,箭頭所指的地方為每個(gè)套圖的地址,這樣我們就可以進(jìn)入套圖,一個(gè)這樣的頁面包含有24個(gè)這樣的套圖,我們用BeautifulSoup,處理。
③我們進(jìn)入套圖,鼠標(biāo)放到40處,右擊,發(fā)現(xiàn)該套圖圖片的最大圖片數(shù)為第十個(gè)span的值,而且每個(gè)套圖的url同①原理相同為套圖的url+第幾張圖片(如下圖3為第二張圖片),最后下載的url由一個(gè)class為main-titleDIV組成。提取img標(biāo)簽的src屬性即可獲得下載鏈接
from bs4 import BeautifulSoup
import requests
import os
#反‘反盜鏈’
header = { 'Referer':'http://www.mzitu.com'}
for x in range(11,20):
#盜取第十一頁到19頁圖片
html_a=requests.get(base_url+str(x),headers=header)
soup_a=BeautifulSoup(html_a.text,features='lxml')
#解析第一個(gè)網(wǎng)頁
pages=soup_a.find('ul',{'id':'pins'}).find_all('a')
#選出a標(biāo)簽,如第二步的箭頭所指的地方
b=1
for y in pages:
if(b%2!=0):
#因?yàn)橐粋€(gè)li標(biāo)簽里面有兩個(gè)a標(biāo)簽,所以要去除重復(fù)
html=requests.get(y['href'],headers=header)
soup_b=BeautifulSoup(html.text,features='lxml')
#進(jìn)入套圖,解析套圖
pic_max=soup_b.find_all('span')[10].text
#選出該套圖的最大圖片數(shù)
tittle=soup_b.find('h2',{'class':'main-title'}).text
os.makedirs('./img/'+str(tittle))
#制造一個(gè)目錄
for i in range(1,int(pic_max)+1):
#循環(huán),下載套圖圖片,
href=y['href']+'/'+str(i)
html2=requests.get(href,headers=header)
soup2=BeautifulSoup(html2.text,features='lxml')
pic_url=soup2.find('img',alt=tittle)
html_name=requests.get(pic_url['src'],headers=header,stream=True)
file_name=pic_url['src'].split(r'/')[-1]
with open('./img/'+str(tittle)+'/'+file_name,'wb') as f:
#按32字節(jié)下載
for x in html_name.iter_content(chunk_size=32):
f.write(x)
b=b+1
print('ok')#判斷程序是否結(jié)束
from bs4 import BeautifulSoup
import requests
import os
import re
base_url='
header = { 'Referer':'http://www.mzitu.com'}
for x in range(61,62):
html_a=requests.get(base_url+str(x),headers=header)
soup_a=BeautifulSoup(html_a.text,features='lxml')
pages=soup_a.find('ul',{'id':'pins'}).find_all('a')
test=re.findall('"href":"(.*?)"',pages)
print(test)
b=1
for y in pages:
if(b%2!=0):
html=requests.get(y['href'],headers=header)
soup_b=BeautifulSoup(html.text,features='lxml')
pic_max=soup_b.find_all('span')[10].text
tittle=soup_b.find('h2',{'class':'main-title'}).text
u=str(tittle).replace(':','').replace('!','').replace('?','').replace(',','').replace(' ','')
os.makedirs('./img/'+u)
for i in range(1,int(pic_max)+1):
href=y['href']+'/'+str(i)
html2=requests.get(href,headers=header)
soup2=BeautifulSoup(html2.text,features='lxml')
pic_url=soup2.find('img',alt=tittle)
html_name=requests.get(pic_url['src'],headers=header,stream=True)
file_name=pic_url['src'].split(r'/')[-1]
with open('./img/'+u+'/'+file_name,'wb') as f:
for x in html_name.iter_content(chunk_size=32):
f.write(x)
b=b+1
print('ok')
以上代碼為原創(chuàng)代碼,
總結(jié)
以上是生活随笔為你收集整理的python爬图片教程_python爬去妹子网整个图片资源教程(最详细版)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cout 数组_C语言学习笔记(十)二维
- 下一篇: chrome java插件_seleni