python抓取bing首頁背景圖片

來源:互聯網
上載者:User

標籤:find   原因   ora   pattern   family   ace   ror   save   .com   

最初Python2寫法:#!/usr/bin/env python

# -*- coding:utf-8 -*-

# -*- author:nancy -*-

# python2抓取bing首頁所有背景圖片

import urllib,re,sys,os

def get_bing_backphoto():

    if (os.path.exists(‘photos‘)== False):

        os.mkdir(‘photos‘)

    for i in range(0,1000):

        url = ‘http://cn.bing.com/HPImageArchive.aspx?format=js&idx=‘+str(i)

               +‘&n=1&nc=1361089515117&FORM=HYLH1‘

        html = urllib.urlopen(url).read()

        if html == ‘null‘:

            print ‘open & read bing error!‘

            sys.exit(-1)

        reg = re.compile(‘"url":"(.*?)","urlbase"‘,re.S)

        text = re.findall(reg,html)

        #http://s.cn.bing.net/az/hprichbg/rb/LongJi_ZH-CN8658435963_1366x768.jpg

        for imgurl in text:

            right = imgurl.rindex(‘/‘)

            name = imgurl.replace(imgurl[:right+1],‘‘)

            savepath = ‘photos/‘+ name

            urllib.urlretrieve(imgurl, savepath)

            print name + ‘ save success!‘

get_bing_backphoto()

Python3與Python2的錯誤調整:TypeError: can‘t use a string pattern on a bytes-like object 原因為Python3 findall資料類型用bytes類型,因此在Regex前應添加html = html.decode(‘utf-8‘)。 “AttributeError: ‘module‘ object has no attribute ‘urlopen‘” 原因是Python3裡的urllib模組已經發生改變,此處的urllib都應該改成urllib.request。由於bing圖片對外介面的圖片json格式變了,python第三方庫的匯入格式有變化,因此代碼調整如下:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# -*- author:nancy-*-

# python3抓取bing首頁所有背景圖片

import urllib.request,re,sys,os

def get_bing_backphoto():

    if (os.path.exists(‘photos‘)== False):

        os.mkdir(‘photos‘)

    for i in range(0,10):

        url = ‘http://cn.bing.com/HPImageArchive.aspx?format=js&idx=‘+str(i)+‘&n=1&nc=1361089515117&FORM=HYLH1‘

        html = urllib.request.urlopen(url).read()

        if html == ‘null‘:

            print( ‘open & read bing error!‘)

            sys.exit(-1)

        html = html.decode(‘utf-8‘)

        html = html.replace(‘/az/‘,‘http://s.cn.bing.net/az/‘)

        reg = re.compile(‘"url":"(.*?)","urlbase"‘,re.S)

        text = re.findall(reg,html)

        for imgurl in text :

            right = imgurl.rindex(‘/‘)

            print(imgurl)

            name = imgurl.replace(imgurl[:right+1],‘‘)

            savepath = ‘photos/‘+ name

            urllib.request.urlretrieve(imgurl, savepath)

            print (name + ‘ save success!‘)

get_bing_backphoto()

python抓取bing首頁背景圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.