This article is participating in “Python Theme Month”, check out the Python writing season for details, and show off your Python article – 2000 yuan for a limited prize

One background image per day

rendering

Run code every day

The original

Hand – to – hand teach you to use the crawler to change wallpaper every day

Setting the Background

The source code

try:
    from requests_html import HTMLSession
    import requests
    import time
    import os
except:
    import os

    os.system('sudo pip3 install requests_html')
    from requests_html import HTMLSession
    import requests
    import time
    import os

session = HTMLSession()
madir = 'C: / Users/Administrator/Desktop/background'  # Image save address
Turnpage = 0  # total number of pages
page = 1  # Number of pages currently crawled


def contrast(title) :
    file = os.listdir(madir)
    iffile ! = [] :if title in file:
            return False
        else:
            return True
    else:
        return True


def get_girl_list(dig) :
    if not os.path.exists(madir):
        os.mkdir(madir)
    url = 'https://bing.ioliu.cn/?p=%s' % dig
    r = session.get(url)
    about = r.html.find('div.progressive')
    global Turnpage
    Turnpage = int(r.html.find('div.page>span', first=True).text.split('/') [1])
    for item in about:
        img = item.find('img', first=True)
        sl = img.attrs['src'].split('_640x480') [0]
        name = sl.split('/')[-1]
        title = name + '.jpg'
        print(contrast(title))
        if contrast(title):
            print('Downloading..... ')
            get_girl_detail(name)
        else:
            print('Picture already exists')

    global page
    page += 1

    if page < Turnpage:
        Turn_the_page(page)


#
def get_girl_detail(name) :
    time.sleep(0.3)  # Milliseconds timestamp
    title = name + '.jpg'

    img_response = session.get('https://cn.bing.com/th?id=OHR.' + name + '_UHD.jpg')
    with open(madir + '/%s' % title, 'wb') as f:
        f.write(img_response.content)

    print(title + 'Download complete')


def Turn_the_page(page) :
    print('This is page %s' % page)
    get_girl_list(page)


if __name__ == "__main__":
    Turn_the_page(page)


Copy the code