zoukankan      html  css  js  c++  java
  • Python下载校花贴吧上面的图片

     涉及:

    获取桌面路径

    创建路径

    获取时间文本

    # write by xdd1997  xdd2026@qq.com
    # 2020-08-07
    
    import time
    import os
    import winreg
    import requests
    import urllib.request
    from bs4 import BeautifulSoup
    
    
    # 获取桌面路径
    def desktop_path():
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'SoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders')
        path = winreg.QueryValueEx(key, "Desktop")[0]
        return path
    # -------------递归创建目录-----------
    def CreatPath():
        path = os.path.join(desktop_path().replace('/','\'),'校花贴吧图片')
        if not os.path.exists(path):
            os.makedirs(path)
        return path
    
    for i in range(0,100,50):  ## 100=50*2,表明下载2页,可改为150,300...
        url = "https://tieba.baidu.com/f?kw=%E6%A0%A1%E8%8A%B1&ie=utf-8&pn={}".format(i)
        print(url)
        r = requests.get(url)
        demo = r.text
        soup = BeautifulSoup(demo, "html.parser")
        piclist = []
        for link in soup.find_all('img'):
            link_list = link.get('bpic')
            if link_list != None:
                piclist.append(link_list)
    
        for http in piclist:
            print(http)
            name = time.strftime("%Y%m%d%H%M%S", time.localtime())
            filesavepath = os.path.join(CreatPath(), name + '.jpg')
            urllib.request.urlretrieve(http, filesavepath)
            print('正在保存图片')
            time.sleep(1)
        print('下载完成')

    转载仅为学习,不会商用。
    欢迎转载原创,附文链接。
  • 相关阅读:
    c#中的as,is和强转
    Shader中的lerp
    [RequireComponent(typeof(....))]
    [ExecuteInEditMode]
    在ugui上显示3d物体
    T4语法快速入门
    MVC生命周期
    MVC5路由系统机制详细讲解
    FluentScheduler定时器计划任务
    MVC的WebViewPage
  • 原文地址:https://www.cnblogs.com/xdd1997/p/13455108.html
Copyright © 2011-2022 走看看