前言
1镊叁、使用python自動從zol下載壁紙抖剿,比較簡單敞咧,高手自動忽略傻铣。
2尤莺、沒進行翻頁的處理吐葱,只單純down了一個頁面的圖片而已秸仙。
3袋坑、整體邏輯,解析縮略圖頁面优幸,自動請求高清url并下載到本地吨拍。
源碼
import requests, bs4, re
# 發(fā)起請求
def myUrlGet(url):
try:
res = requests.get(url)
res.raise_for_status()
return res
except Exception as exc:
print('get %s url failed: %s' % (url, exc))
return
# 保存圖片
def saveImgFile(filename, url):
imgres = myUrlGet(url)
if imgres != "" and len(imgres.content) > 0:
try:
imgFile = open(filename, 'wb')
except Exception as exc:
print('can not open file: %s' % (exc))
return
imgFile.write(imgres.content)
# 獲取壁紙原始地址
def downZolImg(imgname, imgurl):
imgres = myUrlGet(imgurl)
if imgres != None:
soup = bs4.BeautifulSoup(imgres.text)
imgorig = soup.select('#bigImg')
if len(imgorig) > 0:
saveImgFile('img/' + imgname, imgorig[0].attrs['src'])
# 主處理邏輯
if __name__ == '__main__':
zolurl = 'http://desk.zol.com.cn/'
zolres = myUrlGet(zolurl + 'nb/')
if zolres != None:
zolsoup = bs4.BeautifulSoup(zolres.text)
photoList = zolsoup.select('.photo-list-padding')
photoNameRegex = re.compile(r'([^/]+)\.html')
for photo in photoList:
photourl = photo.contents[0].attrs['href']
photoName = photoNameRegex.search(photourl).group(1) + '.jpg'
print(photoName, zolurl + photourl)
downZolImg(photoName, zolurl + photourl)
效果:
image.png