当前位置:天才代写 > tutorial > Python教程 > python 抓取bing主页所有背景图片-python爬虫

python 抓取bing主页所有背景图片-python爬虫

2021-02-28 17:39 星期日 所属: Python教程 浏览:774

bing首页情况url

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

python 完成编码

import urllib.request
import re
import sys
import os


def get_bing_backphoto():
    if os.path.exists('photos') == False:
        os.mkdir('photos')

    for i in range(0, 30):

        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')
        reg = re.compile('"url":"(.*?)","urlbase"', re.S)
        text = re.findall(reg, html)
        # https://uhomework.com/wp-content/uploads/2021/02/20210201072115-6017abeb3a027.jpg
        for imgurl in text:
            right = imgurl.rindex('/')
            name = imgurl.replace(imgurl[:right   1], '')
            savepath = 'photos/'   name
            urllib.request.urlretrieve(imgurl, savepath)
            print(name   ' save success!')

get_bing_backphoto()
 

    关键字:

天才代写-代写联系方式