0x01 Code
import shutil
import requests
import os
from bs4 import BeautifulSoup
url='https://www.woyaogexing.com/touxiang/fengjing/'
def download_tupian(image_url,image_localpath):
response = requests.get(image_url,stream = True)
if response.status_code == 200:
with open(image_localpath,'wb') as f:
response.raw.deconde_content = True
shutil.copyfileobj(response.raw,f)
def pa(url):
response = requests.get(url)
soup = BeautifulSoup(response.text,'lxml')
for divv in soup.find_all('div',class_='pMain'):
for tupian in divv.find_all('img'):
imgurl = tupian.get('src')
newimgurl='http:'+imgurl
dir = os.path.abspath('./images')
filename = os.path.basename(newimgurl)
imgpath = os.path.join(dir,filename)
print ('下载ing: %s' % imgurl)
download_tupian(newimgurl,imgpath)
pa(url)
0x02 效果