Python图形化爬取图片
时间: 2023-12-25 07:28:39 浏览: 73
以下是使用Python进行图形化爬取图片的方法:
```python
import requests
from bs4 import BeautifulSoup
import os
import urllib.request
import tkinter as tk
from tkinter import filedialog
# 创建GUI窗口
root = tk.Tk()
root.withdraw()
# 选择保存路径
folder_path = filedialog.askdirectory()
# 输入要爬取的网页链接
url = input("请输入要爬取的网页链接:")
# 发送请求并解析网页
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有图片链接
img_tags = soup.find_all('img')
# 遍历所有图片链接并下载
for img in img_tags:
img_url = img['src']
if img_url.startswith('//'):
img_url = 'http:' + img_url
elif not img_url.startswith('http'):
img_url = url + '/' + img_url
img_name = img_url.split('/')[-1]
img_path = os.path.join(folder_path, img_name)
urllib.request.urlretrieve(img_url, img_path)
print('图片下载完成!')
```
阅读全文