编写python代码获取4k高清壁纸

Huskiesir最近在研究python爬虫大约俩周了吧,由于比较懒,也没把具体研究的过程与经验写下来,实在是一大憾事。这次直接上干货,代码送给大家:

import re
import requests

url = "https://unsplash.com"
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}

def new_links_get(url):
    res = requests.get(url, headers=headers)
    links = re.findall('<a title=.*?class="_2Mc8_" href="(.*?)">', res.text, re.S)
    #print(len(links))
    #print(links)
    new_links = []
    for link in links:
        new_link = url + link
        new_links.append(new_link)
    return new_links


def imglink_get(new_links):
    img_links = []
    for new_link in new_links:
        res = requests.get(new_link, headers=headers)
        download_link = re.findall('<a title="Download photo" href="(.*?)"', res.text, re.S)[0]
        img_links.append(download_link)
    #print('imglinks:', len(img_links))
    return img_links



def img_get(img_links):
    path = "d://user/01382408/桌面/photo/"
    for imglink in imglinks:
        img_name = imglink.split('/')[4]
        res = requests.get(imglink, headers=headers)
        f = open(path + img_name + '.jpg', 'wb')
        img = f.write(res.content)
    f.close()


if __name__ == "__main__":
    new_links = new_links_get(url)
    imglinks = imglink_get(new_links)
    img_get(imglinks)

 

posted @ 2019-03-22 14:11  huskiesir  阅读(1333)  评论(0编辑  收藏  举报