博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java与Python下载Bing首页图片
阅读量:5110 次
发布时间:2019-06-13

本文共 3185 字,大约阅读时间需要 10 分钟。

Java与Python下载Bing首页图片

一,首先是Java代码

import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;public class DownloadImg {    public static void writeImgEntityToFile(HttpEntity imgEntity,String fileAddress) {        File storeFile = new File(fileAddress);        FileOutputStream output = null;        try {            output = new FileOutputStream(storeFile);            if (imgEntity != null) {                InputStream instream;                instream = imgEntity.getContent();                byte b[] = new byte[8 * 1024];                int count;                while ((count = instream.read(b)) != -1) {                    output.write(b, 0, count);                }            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                output.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }    public static void main(String[] args) {        System.out.println("获取Bing图片地址中……");        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");        CloseableHttpClient httpClient = HttpClients.createDefault();        HttpGet httpGet = new HttpGet("http://cn.bing.com/");        CloseableHttpResponse response = null;        try {            response = httpClient.execute(httpGet);            Pattern p = Pattern.compile("http://.*?\\.jpg");            Matcher m = p.matcher(EntityUtils.toString(response.getEntity()));            String address = null;            if (m.find()) {                address = m.group();            } else {                System.exit(0);            }            System.out.println("图片地址:" + address);            System.out.println("正在下载……");            HttpGet getImage = new HttpGet(address);            CloseableHttpResponse responseImg = httpClient.execute(getImage);            HttpEntity entity = responseImg.getEntity();            writeImgEntityToFile(entity,dateFormat.format(new Date()) + ".jpg");            System.out.println("下载完毕.");        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                httpClient.close();                response.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}

二,Python代码

#!/usr/bin/python#encoding:utf-8import timeimport urllibimport rehtml = urllib.urlopen("http://cn.bing.com/").read()imgAddress = re.search(r"http://.*?\.jpg",html)if imgAddress:    fileName = time.strftime("%Y-%m-%d") + ".jpg"    print "今天Bing图片的地址是:" + imgAddress.group()    print "正在下载……"    urllib.urlretrieve(imgAddress.group(), fileName)    print "下载完毕!" + "存储为" + fileNameelse:    print "今天貌似出问题了……"

转载于:https://www.cnblogs.com/geekgao/p/4531598.html

你可能感兴趣的文章
git中常用的操作命令有哪些?常用操作命令归纳
查看>>
【问底】夏俊:深入站点服务端技术(一)——站点并发的问题
查看>>
NGUI 降低drawcall
查看>>
poj1178 floyd+枚举
查看>>
A2-01-02.Install MySQL
查看>>
leetcode-152-乘积最大子序列
查看>>
微信C# SDK
查看>>
学习笔记78—三大统计相关系数:Pearson、Spearman秩相关系数、kendall等级相关系数...
查看>>
Next Permutation
查看>>
Mybatis深入之事务管理
查看>>
Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
查看>>
Android Bluetooth Stack: Bluedroid(五岁以下儿童):The analysis of A2DP Source
查看>>
android WebView总结
查看>>
选项卡登录
查看>>
Ruby on Rails开发Web应用的基本概念
查看>>
SQLServer日期函数
查看>>
Android事件分发回传机制
查看>>
cocosStudio中使用PageView,ListView和ScrollView
查看>>
简体字繁体字互转
查看>>
1017. A除以B (20)
查看>>