我想写一个python程序将这个页面的标题和图片下载到本地httpsclt254comreadphptid=1569614
以下是一个Python程序,可以从给定的URL中提取标题和下载图片:
import requests
from bs4 import BeautifulSoup
import os
# URL of the page to be scraped
url = 'https://clt254.com/read.php?tid=1569614'
# Send a GET request to the URL
response = requests.get(url)
# Parse the HTML content of the page using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Extract the title of the page
title = soup.title.text.strip()
# Extract the image URL from the page
img_url = soup.find('img')['src']
# Download the image
img_data = requests.get(img_url).content
filename = os.path.basename(img_url)
with open(filename, 'wb') as f:
f.write(img_data)
# Print the title of the page
print(title)
# Print the filename of the downloaded image
print(filename)
该程序使用requests库发出GET请求,并使用BeautifulSoup库解析响应的HTML内容。然后,它提取页面的标题和图像URL,并使用requests库下载图像。最后,程序打印页面标题和下载的图像文件名。请注意,程序使用os库中的basename函数从URL中提取文件名。
原文地址: https://www.cveoy.top/t/topic/b0vi 著作权归作者所有。请勿转载和采集!