当前位置: 首页 > news >正文

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

文章目录

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

一、书籍推荐

推荐本人书籍《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!!")

三、运行结果

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

http://www.lryc.cn/news/170969.html

相关文章:

  • 短剧推广和小说推文在哪里授权介绍
  • Java:本地文件通过表单参数接口发送后大小变成0
  • Linux 共享内存
  • druid在springboot中如何整合配置!
  • 数据结构:栈
  • 每日刷题-6
  • systrace使用注意事项
  • RockyLinux9.2 网卡配置和nmcli、nmtui命令的使用
  • Java线程池ThreadPoolExecutor应用(Spring Boot微服务)
  • QT5|C++|通过信号槽机制实现进度条更新
  • 什么是智能推荐?智能推荐的原理是什么?
  • Windows下的Elasticsearch-head安装
  • 两台服务器间进行文件传输
  • 研究生选控制嵌入式还是机器视觉好?
  • SecureCRT SSH与FTP连接中文乱码
  • OSI七层网络参考模型与数据流通过程
  • 数字孪生行业相关政策梳理--工业领域相关政策(可下载)
  • 【工具】咸鱼之王辅助小助手来了!
  • 黑马JVM总结(十)
  • JPEG、GIF动图可以转换成SVG、Eps格式的矢量图吗?
  • 数据结构与算法的力量:编写更高效的代码
  • Python批量统计pdf中“中文”字符的个数
  • LeetCode的第 363 场周赛——记录+补题
  • 【网络协议】Http-上
  • Langchain-chatchat本地部署
  • SQL故障和排查解决浅析
  • 基础算法--双指针算法
  • 企业工程项目管理系统源码(三控:进度组织、质量安全、预算资金成本、二平台:招采、设计管理)
  • 生物的神经系统与机器的人工神经网络
  • JNI 基础