vi bing_pictures.py
"""A simple script to import the daily picture of bing"""
import urllibimport urllib2
import urllib3
import re
import time
def main():
"""We use the xml api provided by bing to get the pic url"""
hostname = "http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1"
req = urllib2.Request(hostname)
webpage = urllib2.urlopen(req)
content = str(webpage.read())
url_tail = re.search(r'<url>[^\s]*</url>', content)
url = 'http://cn.bing.com' + str(url_tail.group())[5:-6]
print(url)
pic_file_name = time.strftime('%Y_%m_%d', time.localtime(time.time()))
urllib.urlretrieve(url, pic_file_name+url[-4:])
if __name__ == '__main__':
main()
vi bing_xml_pictures.py
"""A simple script to import the daily picture of bing"""
import urllib.request
import xml.etree.ElementTree as ET
import time
def main():
"""We use the xml api provided by bing to get the pic url"""
req = urllib.request.Request("http://cn.bing.com/HPImageArchive.aspx?idx=0&n=1")
webpage = urllib.request.urlopen(req)
root = ET.fromstring(webpage.read())
url = 'http://cn.bing.com'+root.find('image').find('url').text
print(url)
pic_file_name = time.strftime('%Y_%m_%d', time.localtime(time.time()))+\
root.find('image').find('messages').find('message').find('msgtext').text
urllib.request.urlretrieve(url, pic_file_name+url[-4:])
if __name__ == '__main__':
main()
python bing_pictures.py bing_xml_pictures.py