代码在此
[Python] 纯文本查看 复制代码 from requests_html import HTMLSession
import requests
import os
# 需要python3.6 貌似requests-html需要3.6
session = HTMLSession()
WORKDIR = 'D:\\bing_wallpaper'
def download(one_pic_url):
p = session.get(one_pic_url)
p.html.encoding = 'utf-8'
# 图片标题 作者
p_title = p.html.find('div.description', first=True).find('p.title', first=True).text
# 图片副标题 说明
p_sub = p.html.find('div.description', first=True).find('p.sub', first=True).text
# 图片下载地址
p_download_url = base_url + list(p.html.find('div.options', first=True).find('a.download')[0].links)[0]
print("开始下载 {} ..".format(p_title))
# 标题里包含 "/" 导致os.mkdir 报错 暂时还不知道咋解决 所以暂时替换成空格了
pic_dir = "{}\\{}".format(WORKDIR, p_title.replace("/"," "))
if not os.path.exists(pic_dir):
os.mkdir(pic_dir)
readme = "{}\\readme.txt".format(pic_dir)
with open(readme, 'a+') as f:
f.write(p_sub)
pic = requests.get(p_download_url, stream=True)
pic_file = "{}\\pic.jpg".format(pic_dir)
if pic.status_code == 200:
with open(pic_file, 'wb') as p:
p.write(pic.content)
print("完成下载 {}".format(p_title))
if __name__ == '__main__':
if not os.path.exists(WORKDIR):
os.mkdir(WORKDIR)
base_url = "https://bing.ioliu.cn"
sum = 2
for num in range(1, sum):
if num:
url = "{0}/?p={1}".format(base_url, num)
r = session.get(url)
r.html.encoding = 'utf-8'
# 获取当前页面下所有图片的连接
pic_lst = r.html.find('a.mark')
for pic_url in pic_lst:
pic_uri = list(pic_url.links)[0]
# 组合出完整的图片下载地址
one_pic_url = "{0}{1}".format(base_url,pic_uri)
download(one_pic_url)
效果图也上几张
运行
下载之后的目录
图片和描述信息
|