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('下载完成')

    转载仅为学习,不会商用。
    欢迎转载原创,附文链接。
  • 相关阅读:
    多通道 移位寄存器 verilog
    modelsim-altera IP核仿真
    modelsim-altera
    YUV视频显示软件+源码
    opencl教程
    使用VisualStudio读写NI FPGA板卡实例(基于FPGA Interface C API Generator)
    Visual Studio编译与调用DLL方法
    NI FPGA板卡程序设计概述
    LabVIEW中使用GPU进行高性能计算
    Windows平台编程涉及的函数
  • 原文地址:https://www.cnblogs.com/xdd1997/p/13455108.html
Copyright © 2011-2022 走看看