2. 爬取 https://authserver.hnca.edu.cn/authserver/login 页面所有img标签图片,保存到当前python代码的工作目录下面。
时间: 2023-12-26 13:06:48 浏览: 55
好的,可以使用Python的requests和BeautifulSoup库来爬取页面并保存图片。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
import os
# 发送请求获取页面内容
url = 'https://authserver.hnca.edu.cn/authserver/login'
response = requests.get(url)
# 解析页面内容,获取所有img标签
soup = BeautifulSoup(response.text, 'html.parser')
img_tags = soup.find_all('img')
# 遍历所有img标签,下载图片并保存到本地
for img_tag in img_tags:
img_url = img_tag['src']
img_response = requests.get(img_url)
img_name = os.path.basename(img_url) # 获取图片文件名
with open(img_name, 'wb') as f:
f.write(img_response.content)
print(f'Saved {img_name} to current working directory.')
```
这段代码会将所有img标签的图片下载并保存到当前工作目录下。需要注意的是,如果页面中的图片链接是相对链接,需要将其转换为绝对链接。