版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ppdyhappy/article/details/80338529
代码比较简单,功能是实现从bing下载当日的壁纸,程序最后会在py文件同级目录下新建bing文件夹,并把壁纸存放到该文件夹内,代码如下:
# -*- coding:utf-8 -*-
__author__ = 'ppdyhappy'
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logging.disable(logging.DEBUG)
import requests
import os
import bs4
import re
# g_img={url: "/az/hprichbg/rb/OakTreeMaize_ZH-CN10523296117_1920x1080.jpg",id:'bgDiv',d:'200',cN:'_SS',crN:'bIm',hash: "118",del: 50}
BG_IMAGE_URL_FLAG = 'g_img='
def get_bing_bg():
os.chdir('.')
imageUrlRegex = re.compile(r'g_img={url:\s*"(.+jpg)"')
webPath = r'https://cn.bing.com/'
if not os.path.isdir('bing'):
os.makedirs('bing')
res = requests.get(webPath)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "html5lib")
elems = soup.select('script[type="text/rms"]')
logging.debug(len(elems))
cnt = 0
while cnt < len(elems):
elemText = elems[cnt].getText()
if BG_IMAGE_URL_FLAG in elemText:
# logging.debug(elemText)
imageUrlRegexResult = imageUrlRegex.search(elemText)
imageUrl = webPath + imageUrlRegexResult.group(1)[1:]
break
cnt += 1
logging.info(imageUrl)
res = requests.get(imageUrl)
res.raise_for_status()
imageFile = open(os.path.join('bing', os.path.basename(imageUrl)), 'wb')
for chunk in res.iter_content(100000):
imageFile.write(chunk)
imageFile.close()
logging.debug('done')
if __name__ == '__main__':
get_bing_bg()
需要安装第三方模块,使用pip进行下载
pip install requests
pip install beautifulsoup4
安装HTML解析器
pip install html5lib
参考链接:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html