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

相关推荐
HackShendi1 分钟前
记一次小程序爬虫(反编译-自动化字体映射生成)
爬虫·小程序·自动化
查理零世18 分钟前
【算法】 区间合并(附蓝桥杯真题) python
python·算法·蓝桥杯
人工智能研究所1 小时前
使用OpenCV与Python编写自己的俄罗斯方块小游戏
开发语言·python·opencv
DDD小小小宇宙1 小时前
python列表基础知识
开发语言·windows·python
@黄色海岸1 小时前
【sklearn 05】sklearn功能模块
人工智能·python·sklearn
追逐☞2 小时前
PyTorch使用-张量类型转换
人工智能·pytorch·python
懒大王爱吃狼2 小时前
Python + Qt Designer构建多界面GUI应用程序:Python如何调用多个界面文件
开发语言·数据库·python·qt·mysql·python基础·命令模式
北京_宏哥2 小时前
🔥《手把手教你》系列练习篇之8-python+ selenium自动化测试(详细教程)
前端·python·selenium
北京_宏哥2 小时前
🔥《手把手教你》系列练习篇之7-python+ selenium自动化测试(详细教程)
前端·python·selenium
三道杠卷胡2 小时前
【AI News | 20250316】每日AI进展
人工智能·python·语言模型·github·aigc