【python爬虫】—图片爬取

图片爬取

需求分析

Python实现

  • 获取待爬取网页
python 复制代码
def get_htmls(pages=list(range(2, 5))):
    """获取待爬取网页"""
    pages_list = []
    for page in pages:
        url = f"https://pic.netbian.com/4kfengjing/index_{page}.html"
        response = requests.get(url)
        response.encoding = 'gbk'
        pages_list.append(response.text)
    return pages_list
get_htmls(pages=list(range(2, 5)))
  • 获取所有图片,并下载
python 复制代码
def get_picturs(htmls):
    """获取所有图片,并下载"""
    for html in htmls:
        soup = BeautifulSoup(html, 'html.parser')
        pic_li = soup.find('div', id='main').find('div', class_='slist').find('ul', class_='clearfix')
        image_path = pic_li.find_all('img')
        for file in image_path:
            pic_name = './practice05/' + file['alt'].replace(" ",'_') + '.jpg'
            src = file['src']
            src = f"https://pic.netbian.com/{src}"

            response = requests.get(src)

            with open(pic_name, 'wb') as f:
                f.write(response.content)
                print("图片已下载并保存为:{}".format(pic_name))
                
htmls = get_htmls(pages=list(range(2, 5)))
get_picturs(htmls)
  • 爬取结果展示

相关推荐
关山30 分钟前
MCP实战
python·ai编程·mcp
悠哉悠哉愿意1 小时前
【Python语法基础学习笔记】if语句
笔记·python·学习
Q_Q19632884751 小时前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
BYSJMG1 小时前
计算机大数据毕业设计推荐:基于Hadoop+Spark的食物口味差异分析可视化系统【源码+文档+调试】
大数据·hadoop·分布式·python·spark·django·课程设计
杜子不疼.1 小时前
《Python学习之第三方库:开启无限可能》
开发语言·python·学习
青衫客362 小时前
用 Python 实现一个“小型 ReAct 智能体”:思维链 + 工具调用 + 环境交互
python·大模型·llm·react
AI视觉网奇3 小时前
音频分类模型笔记
人工智能·python·深度学习
Ratten4 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(四)配置管理界面和逻辑实现
python
Ratten4 小时前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(五)打包成 exe 应用
python
跟橙姐学代码4 小时前
写 Python 函数别再死抠参数了,这招让代码瞬间灵活
前端·python