• 用Python从网站爬图片


    从极客学院首页爬几张图片:

    一下为titita.txt内容,为极客学院首页源代码节选:

    <div class="jk-uptodate">
        <h2>最新课程</h2>
        <ul>
            
            <li class="uptodate">
                <a href="/zhiye/course/135.html?type=50" target="_blank">
                    <img class="uptodate-img" src="https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20170928/8cc3edeb-0115-43ea-a46f-db6c6e9255ca.jpg" alt="">
                    <p class="uptodate-title">Keras框架入门实战</p>
                    <p class="uptodate-info">
                        初级
                        <span>|</span>5门课
                        
                    </p>
                </a>
            </li>
            
            <li class="uptodate">
                <a href="/zhiye/course/143.html?type=38" target="_blank">
                    <img class="uptodate-img" src="https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20171101/b12ae422-fd63-4b7d-a0d3-13c3ab4479c5.jpg" alt="">
                    <p class="uptodate-title">【实战】Python与消息中间件</p>
                    <p class="uptodate-info">
                        中级
                        <span>|</span>4门课
                        
                    </p>
                </a>
            </li>
            
            <li class="uptodate">
                <a href="/zhiye/course/134.html?type=50" target="_blank">
                    <img class="uptodate-img" src="https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20170928/85a3364e-47a3-41df-b5c8-daf48a57b7cd.jpg" alt="">
                    <p class="uptodate-title">深入浅出 — 自然语言处理</p>
                    <p class="uptodate-info">
                        初级
                        <span>|</span>8门课
                        
                    </p>
                </a>
            </li>
            
            <li class="uptodate">
                <a href="/zhiye/course/145.html?type=18" target="_blank">
                    <img class="uptodate-img" src="https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20171123/9625ede8-31e9-4edc-93e7-74bf5b752585.jpg" alt="">
                    <p class="uptodate-title">Android性能优化-UI篇</p>
                    <p class="uptodate-info">
                        中级
                        <span>|</span>7门课
                        
                    </p>
                </a>
            </li>
            
        </ul>
    </div>

    爬图片的Python程序如下:

    import re
    import requests
    
    f=open('titita.txt','r')
    f1=f.read()
    f.close()
    
    htmls=re.findall('<img class="uptodate-img" src="(.*?)" alt="">',f1,re.S)
    i=0
    for each in htmls:
        print('nowdoloading:'+each)
        pic=requests.get(each)
        fp=open('pic\'+str(i)+'.jpg','wb')
        fp.write(pic.content)
        fp.close
        i+=1
    
    #之前没安装requests,要新建项目

    Pycharm输出为:

    nowdoloading:https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20170928/8cc3edeb-0115-43ea-a46f-db6c6e9255ca.jpg
    nowdoloading:https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20171101/b12ae422-fd63-4b7d-a0d3-13c3ab4479c5.jpg
    nowdoloading:https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20170928/85a3364e-47a3-41df-b5c8-daf48a57b7cd.jpg
    nowdoloading:https://jiuye-res.jikexueyuan.com/zhiye/showcase/attach-/20171123/9625ede8-31e9-4edc-93e7-74bf5b752585.jpg

    爬到的图片在资源管理器中显示为:

  • 相关阅读:
    终端ssh登录mac用shell打包ipa报错:replacing existing signature
    andrond mk通配符遍历文件夹
    一键自动发布ipa(更新svn,拷贝资源,压缩资源,加密图片资源,加密数据文件,加密lua脚本,编译代码,ipa签名,上传ftp)
    (转)C++0x语言新特性一览
    (转)Xcode调试技巧
    (转)关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
    自动编译和提交脚本(结合svn和visual studio)
    (转载)让XCode运行时自动更新资源
    cocos2dx3.0rc导出自定义类到lua的方法
    cocos2dx之lua派生类和方法重新
  • 原文地址:https://www.cnblogs.com/zhangziyan/p/9123031.html
Copyright © 2020-2023  润新知