当前位置:首页 > 编程笔记 > 正文
已解决

爬虫项目(四):抓取网页所有图片

来自网友在路上 172872提问 提问时间:2023-09-20 14:45:17阅读次数: 72

最佳答案 问答题库728位专家为你答疑解惑

文章目录

    • 一、书籍推荐
    • 二、完整代码
    • 三、运行结果

一、书籍推荐

推荐本人书籍《Python网络爬虫入门到实战》 ,详细介绍见👉: 《Python网络爬虫入门到实战》 书籍介绍

二、完整代码

原理:抓取该链接中所有的图片格式。基于selenium来获取,自动下载到output文件夹中。

from selenium import webdriver
import requests as rq
import os
from bs4 import BeautifulSoup
import time# Enter Path : chromedriver.exe
# Enter URL : http://www.netbian.com/meinv/index_2.htmpath = input("Enter Path : ")
url = input("Enter URL : ")
output = "output"def get_url(path, url):driver = webdriver.Chrome(executable_path=r"{}".format(path))driver.get(url)print("loading.....")res = driver.execute_script("return document.documentElement.outerHTML")return resdef get_img_links(res):soup = BeautifulSoup(res, "lxml")imglinks = soup.find_all("img", src=True)return imglinksdef download_img(img_link, index):try:extensions = [".jpeg", ".jpg", ".png", ".gif"]extension = ".jpg"for exe in extensions:if img_link.find(exe) > 0:extension = exebreakimg_data = rq.get(img_link).contentwith open(output + "\\" + str(index + 1) + extension, "wb+") as f:f.write(img_data)f.close()except Exception:passresult = get_url(path, url)
time.sleep(60)
img_links = get_img_links(result)
if not os.path.isdir(output):os.mkdir(output)
for index, img_link in enumerate(img_links):img_link = img_link["src"]print("Downloading...")if img_link:download_img(img_link, index)
print("Download Complete!!")

三、运行结果

如下所示:
在这里插入图片描述

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"爬虫项目(四):抓取网页所有图片":http://eshow365.cn/6-10021-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!