你知道wanimal么?你知道他拍的的小黃圖么?
還是很文藝的,喜歡他拍的tits,breast,so beautiful!!!
哈哈,事情并沒有想象的那么順利,今天解決的事情是正則匹配和python的一點知識。但是主要還是阻擋在,wanimal的網頁可爬,但是圖片服務器不允許,也可能是被墻了,所以圖片暫時還不能保存到本地,做一個悲傷的表情。可能就要放到下次的偽裝瀏覽器里面去解決了。
事實證明是被屏了,開個全局vpn就ok了
# encoding=utf-8
import urllib;
import urllib.request;
import re;
from collections import deque;
class DownLoadPic:#先來個類,用它來下載wanimal的圖嘍
#######################################
#學到點什么呢,就是類里面的這個全局變量更類似于C++里面的static
#是所有類共享的
#哈哈,事實不是這樣的
#self.value這樣訪問變量就不是static,即非共享,
#self.__class__.value這樣訪問才是static,原來還和訪問方式有關!!!
#20161101今天才知道,自由static才在這里定義,也許也不需要在這里定義(沒試),但是非static直接在用的時候self.var就行了
#######################################
__m_deque=deque();#存放將要訪問的下一頁
__m_visited=set();#根據集合的特性,存放已經訪問過網址,包括圖片的和下一頁的
__file=open('e:/forLook.txt','w',encoding='utf-8'); #存放解析出來的網址,就是為了看結果對不對
__adress=open('e:/adress.txt','w',encoding='utf-8');#因為現在圖片不能存在本地,那就先把圖片鏈接存到本地,因為有人說可以用迅雷下載,但是我沒裝迅雷。
__url_init="";#網頁入口
__m_cnt=0;#當前第幾頁
__page_limit=0;#頁數限制
#######################################
#學到點什么呢,這個就是構造函數了,而且類里面所有的函數都必須有參數self
#注意到這些變量和函數前面的__了吧,加了__就是私有的
#######################################
def __init__(self,url_tmp,cnt_limit=99999999):#構造函數
self.__url_init=url_tmp;
self.__m_deque.append(self.__url_init);
self.__m_cnt=1;
self.__page_limit=cnt_limit;
def __del__(self):#析構函數
self.__url_init='';
self.__m_cnt=0;
self.__m_visited={};
self.__m_deque=[];
self.__file.close();
self.__adress.close();
def DLP(self):#有了網址就開始解析下載了
while(self.__m_deque and self.__m_cnt<=self.__page_limit):
#print(self.__m_deque);
cur_url=self.__m_deque.popleft();
self.__m_visited |={cur_url};
print("已經抓取 ",self.__m_cnt," 頁 +++","當前網頁--->",cur_url,"\\n");
self.__m_cnt +=1;
try:
url_opening=urllib.request.urlopen(cur_url);
except:
self.__file.write("網頁打開失敗--->"+cur_url+"\\n");
continue;
if 'html' not in url_opening.getheader('Content-Type'):
continue;
try:
page_data=url_opening.read().decode('utf-8');
except:
self.__file.write("網頁解碼失敗--->"+cur_url+"\\n");
continue;
linkNext='http://wanimal1983.org/page/'+str(self.__m_cnt);#哈哈,先默認從第一頁開始吧,剛開始寫正則太辛苦了
self.__m_deque.append(linkNext);#把下一頁放到待解析隊列
self.__m_visited |={linkNext};#訪問過的網頁
imageDiv=re.compile('<img src=.+?>');#img標簽
imageLink=re.compile('http:.+?\\.jpg');#圖片連接
nameLike=re.compile('[^/]+\\.jpg');#取出圖片名稱
for img in imageDiv.findall(page_data):#取出當前頁所有img標簽
#print(len(imageDiv.findall(page_data)));
imgLink=imageLink.findall(img);#從當前img標簽取出圖片連接
if(1 <= len(imgLink)):
get_img=imgLink[0];
else:
continue;
if 'http' in get_img and get_img not in self.__m_visited:
self.__m_visited |={get_img};
name=nameLike.findall(get_img)[0];#取出圖片名字
self.__file.write("圖片名字-->"+name+"\\n");
self.__file.write("圖片鏈接-->"+get_img+"\\n");
print("正在保存圖片",name,end="==========\\n");
self.__adress.write(get_img+"\\n");
#picFile=open(name,"wb");#暫時直接爬是不行的,好像圖片服務器禁止了簡單爬蟲,那就下一次在偽裝瀏覽器行為,先把鏈接都保存下來,用其他下載器下載吧
#pic=urllib.request.urlopen(get_img).read();
#picFile.write(pic);
#picFile.close();
#調用
a=DownLoadPic("http://wanimal1983.org/",150);
a.DLP();
del a;
所以這么寫算是更標準的格式
# encoding=utf-8
import urllib;
import urllib.request;
import re;
from collections import deque;
class DownLoadPic:
def __init__(self,url_tmp,cnt_start=0,cnt_limit=99999999):#構造函數
self.__url_init=url_tmp;
self.__m_deque=deque();
self.__m_deque=deque();
self.__m_deque.append(self.__url_init);
self.__m_cnt=cnt_start;
self.__page_limit=cnt_limit;
self.__file=open('e:/forLook.txt','w',encoding='utf-8');
def __del__(self):#析構函數
self.__url_init="";
self.__m_cnt=0;
self.__m_visited={};
self.__m_deque=[];
self.__file.close();
def DLP(self):#有了網址就開始解析下載了
while(self.__m_deque and self.__m_cnt<=self.__page_limit):
cur_url=self.__m_deque.popleft();
self.__m_visited |={cur_url};
print("正在抓取 ",self.__m_cnt," 頁 +++","當前網頁--->",cur_url,"\\n");
self.__m_cnt +=1;
try:
url_opening=urllib.request.urlopen(cur_url);
except:
self.__file.write("網頁打開失敗--->"+cur_url+"\\n");
continue;
if 'html' not in url_opening.getheader('Content-Type'):
continue;
try:
page_data=url_opening.read().decode('utf-8');
except:
self.__file.write("網頁解碼失敗--->"+cur_url+"\\n");
continue;
linkNext='http://wanimal1983.org/page/'+str(self.__m_cnt);
self.__m_deque.append(linkNext);#把下一頁放到待解析隊列
self.__m_visited |={linkNext};#訪問過的網頁
imageDiv=re.compile('<img src=.+?>');#img標簽
imageLink=re.compile('http:.+?\\.jpg');#圖片連接
nameLike=re.compile('[^/]+\\.jpg');#取出圖片名稱
for img in imageDiv.findall(page_data):#取出當前頁所有img標簽
#print(len(imageDiv.findall(page_data)));
imgLink=imageLink.findall(img);#從當前img標簽取出圖片連接
if(1 <= len(imgLink)):
get_img=imgLink[0];
else:
continue;
if 'http' in get_img and get_img not in self.__m_visited:
self.__m_visited |={get_img};
name=nameLike.findall(get_img)[0];#取出圖片名字
self.__file.write("圖片名字-->"+name+"\\n");
self.__file.write("圖片鏈接-->"+get_img+"\\n");
print("正在保存圖片",name,end="==========\\n");
picFile=open('e:/'+name,"wb");
pic=urllib.request.urlopen(get_img).read();
picFile.write(pic);
picFile.close();
#調用
if __name__=='__main__':
a=DownLoadPic("http://wanimal1983.org/",150);
a.DLP();