国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Bing圖片下載器(Python實現)

2019-11-14 17:31:39
字體:
來源:轉載
供稿:網友
分享一個Python實現的Bing圖片下載器。下載首頁圖片并保存到到當前目錄。其中用到了正則庫re以及Request庫。
大致流程如下:
1、Request抓取首頁數據
2、re正則匹配首頁圖片URL
3、再次使用Request下載圖片數據
 
源碼: 
# --*-- encoding: UTF-8 --*--

"""
bingloader.py
下載Bing.com首頁圖片
"""

import re
import sys
import os
import requests


# 解析獲取Bing首頁
url = 'http://cn.bing.com/'
PRint("Request Bing.com")
bingweb = requests.get(url=url)
f = open('test.html','w')
f.write(bingweb.text)
f.close()

# 搜索圖片關鍵字
pattern = r'g_img={url:/'(http.*jpg)/',id:/'bgDiv/','
m = re.search(pattern, bingweb.text)
if m:
    picurl = m.group(1)
    print("Picture url:/n{0}".format(picurl))
else:
    print("Not Found picture url.")
    sys.exit(-1)

filename = os.path.basename(picurl)
print('File name:%s' % filename)
if os.path.isfile(filename):
    print("The Picture [%s]' has download." % filename)
    raw_input("Press any key.")
    sys.exit(0)

# 下載圖片數據
print("Download Picture...")
data = requests.get(picurl,stream=True)
with open(filename, 'wb') as picfile:
        for chunk in data.iter_content(chunk_size=1024):
            if chunk: # filter out keep-alive new chunks
                picfile.write(chunk)
                picfile.flush()
        picfile.close()

print("Finished.")
raw_input("Press any key.") 

 



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝州市| 淮南市| 宜城市| 桑植县| 华池县| 太康县| 牟定县| 舟山市| 高安市| 巴楚县| 海安县| 沙洋县| 三都| 介休市| 武义县| 体育| 工布江达县| 同德县| 新竹市| 喀什市| 英山县| 安阳市| 睢宁县| 德州市| 拜泉县| 凤庆县| 西昌市| 丰台区| 苍南县| 凤凰县| 古蔺县| 襄垣县| 太白县| 光山县| 连云港市| 思南县| 泰宁县| 开江县| 长春市| 太康县| 南江县|