Python 获取网页内视频或图片

使用BeautifulSoup 和 Requests,来获取网页内的视频和图片。

import requests
from bs4 import BeautifulSoup

# Send request to get HTML
url = "https://example.com"
response = requests.get(url)
html = response.text

# Parse HTML
soup = BeautifulSoup(html, "html.parser")

# Find all images
images = soup.find_all("img")

# Download images
for image in images:
    image_url = image["src"]
    response = requests.get(image_url)
    with open(image_url.split("/")[-1], "wb") as f:
        f.write(response.content)

类似的代码获取网页里面的视频,只需要更换soup.find_all("img")即可soup.find_all("video")

不同的网页可能有不同的HTML结构,你需要根据实际情况。

猜你喜欢

转载自blog.csdn.net/jackwu11/article/details/129164371