リクエストの実践01_ダウンロードコンピュータの壁紙1080 * 2560

コードを添付

# !usr/bin/env python
# -*- coding:utf-8 -*-
"""
author: Tceo1
time : 2020/1/22 15:09:25
下载2560*1080的壁纸
下载地址:https://2560x1080.com/
"""

import requests
import re

# 1代表第一页
url = "https://2560x1080.com/page/1/"
response = requests.get(url)


# href="https://2560x1080.com/tokyo-blue-2560x1080-wallpaper/" title="Tokyo Blue (2560×1080 Wallpaper)">
# 分组第一个是得到外层的url前半部分,后面的分组是得到图片名称
regStr = r'href="(.*?)2560x1080-wallpaper/" title="(.*) \('
# 拿到首页列表所有图片的URL
allImageUrl = re.findall(regStr, response.text)

for i in allImageUrl:
    # 拼凑列表中每张图片的url:访问后可查看到大图
    listImageUrl = i[0] + "2560x1080-wallpaper/"
    imageName = i[1]
    rsp = requests.get(listImageUrl)
    # <form action="https://2560x1080.com/wp-content/uploads/2019/03/tokyo-blue-2560x1080.jpg" method="get">
    reg2Str = r'<form action="(.*?)"'
    # 拿到高清大图的url 2560*1080
    imageUrl = re.findall(reg2Str, rsp.text)[0]
    print(imageUrl)
    # 拿到图片后缀名
    imageType = imageUrl[-4:]
    print(imageType)
    # 请求图片url返回二进制文件
    imageContent = requests.get(imageUrl).content
    # 图片保存路径
    savePath = r'/home/ty/图片/{0}{1}'.format(imageName, imageType)

    # 写入二进制内容到对应文件
    with open(savePath, 'wb') as f:
        f.write(imageContent)

レンダリング

1 AM2O1.png

送信元アドレス

https://github.com/Tceo1/scriptTools/blob/master/wallpaper.py

おすすめ

転載: www.cnblogs.com/thloveyl/p/12228668.html