【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)
  • 爬取结果展示

相关推荐
Blossom.1184 分钟前
移动端部署噩梦终结者:动态稀疏视觉Transformer的量化实战
java·人工智能·python·深度学习·算法·机器学习·transformer
AiXed35 分钟前
PC微信协议之AES-192-GCM算法
前端·数据库·python
灵光通码1 小时前
神经网络基本概念
python·神经网络
Petrichor_H_3 小时前
DAY 31 文件的规范拆分和写法
python
q***31833 小时前
爬虫基础之爬取某基金网站+数据分析
爬虫·数据挖掘·数据分析
咚咚王者4 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
深蓝海拓4 小时前
使matplot显示支持中文和负号
开发语言·python
AntBlack5 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e5 小时前
搭建本地deepseek大模型
python
1***Q7845 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python