栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬虫中的图片爬取

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬虫中的图片爬取

爬虫中的图片爬取 操作步骤

    指定url

    发出请求

    获取响应数据

    持久化存储

实例1

//单张图片爬取

import requests
url = "https://p5.ssl.qhimgs1.com/sdr/400__/t01df72ed01439728e5.jpg"
response = requests.post(url=url).content  #content表示以二进制的形式返回数据
with open('./国旗.jpg', "wb") as fp:  #图片的形式需定义为 .jpg 格式
    fp.write(response)

点击此处查看图片原图

实例2

//多张图片爬取

import requests
import os
from lxml import etree
if not os.path.exists('./风景'):
    os.mkdir("./风景")
url = "https://pic.netbian.com/4kfengjing/"
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Edg/95.0.1020.40"
​
}
response = requests.get(url=url, headers=headers)
page_text = response.text
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@]//li')
​
for li in li_list:
    url1="https://pic.netbian.com"+li.xpath(".//img/@src")[0]
    name=li.xpath(".//img/@alt")[0]+".jpg"
    name= name.encode("iso-8859-1").decode("gbk")
    response1=requests.get(url=url1, headers=headers)
    content1=response1.content
    img_name="风景/"+name
    with open(img_name, "wb") as fp:
        fp.write(content1)
        print(name, url1)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号