批量爬取图片Python代码
时间: 2023-11-05 19:11:11 浏览: 76
以下是一个简单的Python代码,可以用来批量爬取图片:
```python
import requests
from bs4 import BeautifulSoup
import os
# 获取页面的HTML代码
def get_html(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
# 获取图片链接并下载图片
def download_img(url, root):
try:
r = requests.get(url)
r.raise_for_status()
# 获取图片名称
path = os.path.join(root, url.split("/")[-1])
# 写入文件
with open(path, "wb") as f:
f.write(r.content)
print("图片下载完成!")
except:
print("图片下载失败!")
# 爬取图片
def crawl_images(url, root):
html = get_html(url)
soup = BeautifulSoup(html, "html.parser")
# 获取所有的图片链接
img_links = soup.find_all("img")
for link in img_links:
img_url = link.get("src")
if img_url and img_url.startswith("http"):
download_img(img_url, root)
if __name__ == "__main__":
url = input("请输入要爬取的网页URL:")
root = input("请输入图片保存路径:")
crawl_images(url, root)
```
运行代码后,程序会要求输入要爬取的网页URL和图片保存路径。程序会自动爬取网页中所有的图片链接,并将图片保存到指定路径下。
阅读全文