Python爬取網站美女照片
阿新 • • 發佈:2018-11-11
上次無意之中看到一個網站,裡面全是美女的照片,我就心想,哪天有時間了得把這網站的所有美女照片都得爬下來。今天有時間,寫了點程式碼,爬去了網站的所有照片。附上戰果!圖片實在是太多了,爬了一個多小時,還在爬....
先附上所有的原始碼:
# -*- coding: utf-8 -*- """ Created on Fri Nov 9 17:07:44 2018 @author: 小謝 """ import requests from bs4 import BeautifulSoup import os import random import csv import time urls=[] datas=[] i=0 def Download(name,url,dirname): dir=dirname+"//" path=os.path.join(dir,name) print(path) response=requests.get(url) try: with open(path,"wb") as f: f.write(response.content) f.close() global i i=i+1 except Exception as e: print(e) #獲取每一個分類的URL和名字 def Geturl(): resp=requests.get("http://www.27270.com/ent/meinvtupian/") resp.encoding="gbk" #設定網頁編碼 html=resp.text soup=BeautifulSoup(html,"html.parser") divSoup1=soup.find("div",attrs={"id":"NewTagListBox"}) aas=divSoup1.find_all("a") for a in aas: tup=(a['href'],a.string) urls.append(tup) #將主頁面的各個分欄的連結和名字加入urls元組中 def GetImages(url,dirname): if os.path.exists(dirname): pass else: os.mkdir(dirname) #建立目錄 resp=requests.get(url) resp.encoding="gbk" #設定網頁編碼 html=resp.text soup=BeautifulSoup(html,"html.parser") divSoup=soup.find("ul",attrs={'id':'Tag_list'}) lis=divSoup.find_all("li") file=open("meinv.csv","a",newline="") csv_writer=csv.writer(file) for li in lis: img=li.find("img") alt=img['alt'] name=alt+".jpg" #圖片的名字 src=img['src'] #圖片的下載地址 tup=(name,src) datas.append(tup) for data in datas: csv_writer.writerow(data) Download(data[0],data[1],dirname) file.close() def main(): Geturl() for url in urls: ur=url[0][:-5] #將每個分欄的url連結去除最後的 .html for i in range(11): i+=1 if i==1: uuu=ur+".html" try: GetImages(uuu,url[1]) except Exception as e: print("異常物件的型別是:%s"%type(e)) print("異常物件的內容是:%s"%type(e)) else: uuu=ur+"_"+str(i)+"html" try: GetImages(uuu,url[1]) except Exception as e: print("異常物件的型別是:%s"%type(e)) print("異常物件的內容是:%s"%type(e)) start=time.time() main() end=time.time() miao=end-start i=str(i) print("一共爬去了%s張圖片,花費了%s秒的時間!"%(i,miao))
網站連結:http://www.27270.com/ent/meinvtupian/
爬取網站的第一步,就是先分析網站的結構。我們可以看到,上面這裡有分類
我們右鍵檢查元素,發現這些分類都有規律
我們寫一個函式獲得每個分類的連結和名字,將連結和名字以元組的形式儲存在我們的全域性變數 urls中
def Geturl(): resp=requests.get("http://www.27270.com/ent/meinvtupian/") resp.encoding="gbk" #設定網頁編碼 html=resp.text soup=BeautifulSoup(html,"html.parser") divSoup1=soup.find("div",attrs={"id":"NewTagListBox"}) aas=divSoup1.find_all("a") for a in aas: tup=(a['href'],a.string) urls.append(tup) #將主頁面的各個分欄的連結和名字加入urls元組中
然後我們開始分析每一個分類的規律了,每一個分類都有很多欄,而每一個欄的url都有規律。
http://www.27270.com/tag/875.html
http://www.27270.com/tag/875_2.html
http://www.27270.com/tag/875_3.html
......
所有我們得構造每一個分類的連結,因為每一個分類的欄目數量不同,所以我們選了最多的11
def main(): Geturl() for url in urls: ur=url[0][:-5] #將每個分欄的url連結去除最後的 .html for i in range(11): i+=1 if i==1: uuu=ur+".html" try: GetImages(uuu,url[1]) except Exception as e: print("異常物件的型別是:%s"%type(e)) print("異常物件的內容是:%s"%type(e)) else: uuu=ur+"_"+str(i)+"html" try: GetImages(uuu,url[1]) except Exception as e: print("異常物件的型別是:%s"%type(e)) print("異常物件的內容是:%s"%type(e))
接下來的函式是獲取圖片的URL和名字,
def GetImages(url,dirname):
if os.path.exists(dirname):
pass
else:
os.mkdir(dirname) #建立目錄
resp=requests.get(url)
resp.encoding="gbk" #設定網頁編碼
html=resp.text
soup=BeautifulSoup(html,"html.parser")
divSoup=soup.find("ul",attrs={'id':'Tag_list'})
lis=divSoup.find_all("li")
file=open("meinv.csv","a",newline="")
csv_writer=csv.writer(file)
for li in lis:
img=li.find("img")
alt=img['alt']
name=alt+".jpg" #圖片的名字
src=img['src'] #圖片的下載地址
tup=(name,src)
datas.append(tup)
for data in datas:
csv_writer.writerow(data)
Download(data[0],data[1],dirname)
file.close()
最後這個函式是下載圖片
def Download(name,url,dirname):
dir=dirname+"//"
path=os.path.join(dir,name)
print(path)
response=requests.get(url)
try:
with open(path,"wb") as f:
f.write(response.content)
f.close()
global i
i=i+1
except Exception as e:
print(e)