python3下載必應的圖片

  必應搜索的網址爲:https://cn.bing.com/,該網站的背景圖片會每日更新,使用python3獲取該網站的html源碼,解析出背景圖片,保存到指定目錄,並重命名爲當前日期。

#!/usr/bin/python3
# get the picture from Bing
import re
import requests
import datetime

url = 'https://cn.bing.com/'
# directory to save the picture
dir = 'C:\\Users\\Liangjin\\Pictures\\Saved Pictures\\'

# get the source code of the web page
html = requests.get(url).text
# parse out the picture name
Nurl = re.findall('id="bgLink" rel="preload" href="(.*?)&', html, re.S)
for name in Nurl:
    # the full path of the remote picture
    picture = url + name
    print(picture)
    # save the picture
    pic = requests.get(picture)
    file = dir + str(datetime.datetime.now().year)+'-'+str(
        datetime.datetime.now().month)+'-'+str(datetime.datetime.now().day) + '.jpg'
    fp = open(file, 'wb')
    fp.write(pic.content)
    fp.close()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章