Bilibili 文章图片爬取工具 - 自动下载所有文章图片
import os import time import requests from selenium import webdriver from selenium.webdriver.common.by import By from bs4 import BeautifulSoup
设置运行模式
RUNNING_MODE = 1
设置保存路径
save_path = 'path/to/save/images'
设置文章链接
article_url = 'https://www.bilibili.com/read/cv3094491/?from=search&spm_id_from=333.337.0.0'
if RUNNING_MODE == 1: driver = webdriver.Chrome() print(f'开始抓取页面所有文章,请稍候..') driver.get(article_url) time.sleep(1) soup = BeautifulSoup(driver.page_source, 'html.parser') article_urls = [a['href'] for a in soup.select('a[target='_blank']') if 'https://www.bilibili.com/read/' in a['href']] print(f'本次共扫描到 {len(article_urls)} 篇文章!')
total_url_count = len(article_urls)
current_url_count = 0
for article_url in article_urls:
current_url_count += 1
print(f'正在加载第 {current_url_count}/{total_url_count} 篇文章内容...')
driver.get(article_url)
time.sleep(1)
soup = BeautifulSoup(driver.page_source, 'html.parser')
image_elements = soup.select('img[data-src]')
total_img_count = len(image_elements)
print(f'当前页面共检测到 {total_img_count} 张图片!')
current_img_count = 0
for image_element in image_elements:
image_url = image_element['data-src']
filename = image_url.split('/')[-1]
save_file_path = os.path.join(save_path, f'{filename}')
if os.path.exists(save_file_path):
print(f'图片已存在,跳过下载。路径:{save_file_path}')
time.sleep(0.2)
else:
try:
with open(save_file_path, 'wb') as f:
response = requests.get(image_url)
f.write(response.content)
current_img_count += 1
print(f' [{current_img_count}/{total_img_count}] in [article:{current_url_count}] - 已下载图片:{filename},保存路径:{save_file_path}')
time.sleep(0.1)
except requests.RequestException as e:
print(f'请求错误: {e}')
print(f'等待十秒进行重试')
time.sleep(10)
continue
driver.quit()
原文地址: https://www.cveoy.top/t/topic/hKCP 著作权归作者所有。请勿转载和采集!