Python下载校花贴吧上面的图片

 涉及:

获取桌面路径

创建路径

获取时间文本

 

# write by xdd1997  [email protected]
# 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'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell 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('下载完成')

 

你可能感兴趣的:(Python下载校花贴吧上面的图片)