由於是只用標准庫,裝了python3運行本代碼就能下載到多多的美女圖...
寫出代碼前面部分的時候,我意識到自己的函數設計錯了,強忍繼續把代碼寫完。
測試發現速度一般,200K左右的下載速度,也沒有很好的錯誤處理。不過還是基本上能用。以后繼續改進。
寫出抓取東西的程序,比寫其它程序要開心很多。^_^,大家也來試試寫一個?
"""
python抓取性感尤物美女圖.py
2016年5月4日 00:51:00 codegay
參考資料: Python3學習筆記(urllib模塊的使用)
http://www.cnblogs.com/Lands-ljk/p/5447127.html
以下例子是python2的代碼,並且用到lxml,requests 庫
我用python3標准庫和正則寫一個下載全站美女圖的程序
使用python來批量抓取網站圖片
http://www.cnblogs.com/TeyGao/p/5225940.html
"""
print("程序運行中...")
import re
from urllib import request
import os
from pprint import pprint
from time import sleep
rooturl="http://www.xgyw.cc/"
def getclass():
rec=re.compile('''align=center\>\<a href="(/\w+/)\"\>(.+)\</a\>''')
try:
txt=request.urlopen(rooturl).read().decode("gbk")
fl=rec.findall(txt)
except:
print("錯誤")
sleep(1)
print("分類:")
pprint(fl)
return fl
fenlei=getclass()#下載所有分類下的圖片
#fenlei=[getclass()[-1]]#只下載推女郎
def getpagelist():
plist=[]
for f,n in fenlei:
rec=re.compile('''({}page_\d+?\.html)'''.format(f))
try:
txt=request.urlopen(rooturl+f).read().decode("gbk")
t=sorted(set(rec.findall(txt)+[f]))
plist+=t
except:
print("錯誤",e)
sleep(1)
#print("page_list:")
#pprint(plist)
return plist
pagelist=getpagelist()
def getalbumlist():
albumlist=[]
for r in pagelist:
print(rooturl+r)
try:
txt=request.urlopen(rooturl+r).read().decode("gbk")
for x in re.findall(r'''href=(/(\w+)/(\2)\d+.html)''',txt):
albumlist+=[x[0]]
except:
print("getalbumlist錯誤")
sleep(1)
return albumlist
albumlist=getalbumlist()
def getfphoto():
for r in albumlist:
try:
txt=request.urlopen(rooturl+r).read().decode("gbk")
result=re.findall(r'''(/(\w+)/(\2)\d+_?\d*.html)''',txt)
except:
sleep(1)
pass
for x in result:
try:
html=request.urlopen(rooturl+x[0]).read().decode("gbk")
jpgresult=re.findall('''src=\"(/uploadfile.*?\d+/\w+\.jpg)\"''',html)
print(jpgresult)
except:
sleep(1)
for h in jpgresult:
try:
request.urlretrieve(rooturl+h,os.path.basename(h))
except:
print(3)
sleep(1)
getfphoto()