本来想做成一个Bing美图的主题站的,后来发现网上已经有成熟的了。于是把自己写的自动抓取Bing美图的Python脚本放出

  • 支持Bing中国版和Bing国际版
  • 支持采集国际版的动态背景(已死,原因不明)
  • 图像,介绍都可传到七牛
  • 图像简介,搜索链接可存入MySQL数据库
    使用前请先安装七牛sdk:pip install qiniu和MySQLdb
    中国版采集:

    #!/usr/bin/python
    import urllib2
    import json
    import time
    import MySQLdb
    from qiniu import Auth, put_file, etag, urlsafe_base64_encode, BucketManager
    import qiniu.config
    def imgDownload():

    print "Loaing json data from bing..."
    jsonContent = urllib2.urlopen('http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&&pid=hp')
    jsonData = jsonContent.read()
    s = json.loads(jsonData)
    imageUrl = s['images'][0]['url']
    print "Downloading image..."
    imgData = urllib2.urlopen(imageUrl)
    imgWrite = imgData.read() 
    fileName= time.strftime("%Y%m%d", time.localtime())
    with open("/home/bing/bing.jpg", "wb") as code:     
           code.write(imgWrite)
       with open("/home/bing/info.json", "wb") as code:     
           code.write(jsonData)
       conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',port=3306,read_default_file="/tmp/mysql.sock")
    cur=conn.cursor()     
    conn.select_db('hdlcom_diao')
    cur.execute('SET NAMES utf8;')
    dis = s['images'][0]['copyright'].encode('utf-8')
    se =s['images'][0]['copyrightlink'].encode('utf-8')
    videoUrl = ''
    value=[fileName,'cn',dis,se,imageUrl,videoUrl,'false']
    cur.execute('insert into bing(`date`, `area`, `info`, `search`, `img`, `vid`, `video`) values(%s,%s,%s,%s,%s,%s,%s)',value)
    conn.commit()
    cur.close()
    conn.close()
    

    def upToQiniu():

    print "Uploaing image to qiniu..."
    access_key = ''
    secret_key = ''
    q = Auth(access_key, secret_key)
    bucket_name = '空间名'
    key = 'bing.jpg';
    bucket = BucketManager(q)
    print "Deleting old image..."
    ret, info = bucket.delete(bucket_name, key)
    assert ret == {}
    print "Uploading images to qiniu..."
    fileName= time.strftime("%Y%m%d", time.localtime())
    token = q.upload_token(bucket_name, key, 3600)
    localfile = '/home/bing/bing.jpg'
    ret, info = put_file(token, key, localfile)
    token1 = q.upload_token(bucket_name, fileName+"_cn.jpg", 3600)
    localfile1 = '/home/bing/bing.jpg'
    ret1, info1 = put_file(token1, fileName+"_cn.jpg", localfile1)
    token2 = q.upload_token(bucket_name, fileName+"_cn.json", 3600)
    localfile2 = '/home/bing/info.json'
    ret1, info1 = put_file(token2, fileName+"_cn.json", localfile2)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)
    

    imgDownload()
    upToQiniu()
    </pre> 国际版采集: <pre class="lang:python decode:true" title="国际版 ">#!/usr/bin/python

    import urllib2
    import json
    import time
    import MySQLdb
    from qiniu import Auth, put_file, etag, urlsafe_base64_encode, BucketManager
    import qiniu.config
    def imgDownload():

    print "Loaing json data from bing..."
    jsonContent = urllib2.urlopen('http://global.bing.com/HPImageArchive.aspx?format=js&amp;idx=-1&amp;n=1&amp;pid=hp&amp;FORM=HPCNEN&amp;setmkt=en-us&amp;setlang=en-us&amp;video=1')
    jsonData = jsonContent.read()
    s = json.loads(jsonData)
    try:
        videoUrl =  "http:"+s['images'][0]['vid']['sources'][1][2]
        video = 'true'
    except:
        video = 'false'
    finally:
        videoUrl=''
        imageUrl =  "http://global.bing.com/"+s['images'][0]['url']
    print "Downloading image..."
    imgData = urllib2.urlopen(imageUrl)
    imgWrite = imgData.read() 
    with open("/home/bing/bing.jpg", "wb") as code:     
           code.write(imgWrite)
       with open("/home/bing/info.json", "wb") as code:     
           code.write(jsonData)
       if (video == 'true'):
           print videoUrl
           vidData = urllib2.urlopen(videoUrl)
        vidWrite = vidData.read()
        with open("/home/bing/bing.mp4", "wb") as code:     
               code.write(vidWrite)
    print "Uploaing image to qiniu..."
    access_key = 'AK'
    secret_key = 'SK-'
    q = Auth(access_key, secret_key)
    bucket_name = '空间名'
    key = '/home/bing/bing_en.jpg';
    bucket = BucketManager(q)
    print "Uploading images to qiniu..."
    fileName= time.strftime("%Y%m%d", time.localtime())
    token1 = q.upload_token(bucket_name, fileName+"_en.jpg", 3600)
    localfile1 = '/home/bing/bing.jpg'
    ret1, info1 = put_file(token1, fileName+"_en.jpg", localfile1)
    token2 = q.upload_token(bucket_name, fileName+"_en.json", 3600)
    localfile2 = '/home/bing/info.json'
    ret2, info2 = put_file(token2, fileName+"_en.json", localfile2)
    if(video == 'true'):
        token3 = q.upload_token(bucket_name, fileName+"_en.mp4", 3600)
        localfile3 = '/home/bing/bing.mp4'
        ret2, info2 = put_file(token3, fileName+"_en.mp4", localfile3)
    conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',port=3306,read_default_file="/tmp/mysql.sock")
    cur=conn.cursor()     
    conn.select_db('hdlcom_diao')
    dis = s['images'][0]['copyright']
    se =s['images'][0]['copyrightlink']
    value=[fileName,'en',dis,se,imageUrl,videoUrl,video]
    cur.execute('insert into bing(`date`, `area`, `info`, `search`, `img`, `vid`, `video`) values(%s,%s,%s,%s,%s,%s,%s)',value)
    conn.commit()
    cur.close()
    conn.close()
    

    imgDownload()

注意:使用前请先更改代码中的七牛ak,sk,空间名,MySQL帐号,图像储存的绝对路径
数据库结构如下

`
CREATE TABLE IF NOT EXISTS `bing` (
  `id` int(11) NOT NULL,
  `date` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `area` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `info` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `search` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `img` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `vid` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  `video` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `bing`
--
ALTER TABLE `bing`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `bing`
--
ALTER TABLE `bing`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
`