要批量提取HTML中的圖片,可以使用Python的BeautifulSoup庫,以下是詳細步驟:

1、安裝所需庫
確保已經安裝了beautifulsoup4和requests庫,如果沒有安裝,可以使用以下命令安裝:
pip install beautifulsoup4 requests
2、導入庫
在Python腳本中,導入所需的庫:
from bs4 import BeautifulSoup
import requests
3、獲取HTML內容
使用requests庫獲取網頁的HTML內容:
url = 'https://example.com' # 替換為你要提取圖片的網址
response = requests.get(url)
html_content = response.text
4、解析HTML
使用BeautifulSoup解析HTML內容:
soup = BeautifulSoup(html_content, 'html.parser')
5、提取圖片鏈接
從解析后的HTML中提取圖片鏈接:
img_tags = soup.find_all('img')
img_urls = [img['src'] for img in img_tags if 'src' in img.attrs]
6、下載圖片
將提取到的圖片鏈接下載到本地:
import os
def download_image(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as f:
f.write(response.content)
save_dir = 'images' # 保存圖片的文件夾
os.makedirs(save_dir, exist_ok=True)
for img_url in img_urls:
img_name = img_url.split('/')[1]
save_path = os.path.join(save_dir, img_name)
download_image(img_url, save_path)
將以上代碼整合到一個Python腳本中,運行后即可批量提取HTML中的圖片并保存到本地。
本文名稱:批量html提取圖片_HTML輸入
文章路徑:
http://www.5511xx.com/article/cdoeiso.html