Python爬虫实战:使用requests和BeautifulSoup批量下载图片
import requests
from bs4 import BeautifulSoup
import os
def download(url):
'''下载图片并保存到本地。'''
res = requests.get(url)
img_data = res.content
filename = os.path.join('img1', url.split('/')[-1])
if not os.path.exists(filename):
with open(filename, 'wb') as f:
f.write(img_data)
def get_html():
'''获取网页内容并解析图片链接。'''
for i in range(3, 20): # 获取第2到19页的图片
url = f'http://www.netbian.com/index_{i}.htm'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'}
res = requests.get(url, headers=headers)
res.encoding = 'gbk'
if res.status_code == 200:
html = res.text
soup = BeautifulSoup(html, 'lxml')
all_list = soup.find(class_ = 'list')
all_imgs = all_list.find_all('img')
if not os.path.exists('img1'):
os.makedirs('img1')
for img in all_imgs:
src = img['src']
download(src)
if __name__ == '__main__':
get_html()

原文地址: http://www.cveoy.top/t/topic/fR7h 著作权归作者所有。请勿转载和采集!