旧瓶新酒-获取网络资源即爬取下载页面内容(图片、html、css、js等)
这个java获取网络资源以前也写过不少
最近用到又重新写了一个,apache.commons.io中的例子就非常好,但是无法对请求进行详细设置
于是大部分照搬,局部替换以设置请求头
如需更加复杂的设置,可以考虑使用同为apche的httpComponents
**
```java
package boot.example;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
Created by wq on 2017/6/6.
*/
public class Download {
public static void main(String[] args) {
Download download = new Download();
String url = "http://img1.3lian.com/2015/w7/85/d/21.jpg";
String path1 = "E:\1.jpg";
String path2 = "E:\2.jpg";
String path3 = "E:\3.jpg";
String url2="http://www.baidu.com";
try {
download.apacheCommonsIoDownload(url, path1);
} catch (Exception e) {
e.printStackTrace();
}
try {
download.pureJavaNetDownload(url, path2);
} catch (Exception e) {
e.printStackTrace();
}
try {
download.mixedDownload(url, path3);
} catch (Exception e) {
e.printStackTrace();
}
try{
download.getContentAsString(url2);
}catch (Exception e){
e.printStackTrace();
}
}private void apacheCommonsIoDownload(String urlstr, String path) throws Exception {
apacheCommonsIoDownload(urlstr, new File(path));
}private void apacheCommonsIoDownload(String urlstr, File file) throws Exception {
FileUtils.copyURLToFile(new URL(urlstr), file);
}private void pureJavaNetDownload(String urlstr, String path) throws Exception {
pureJavaNetDownload(urlstr, new File(path));
}//无需依赖
private void pureJavaNetDownload(String urlstr, File file) throws Exception {
URL url = new URL(urlstr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
//有的网站屏蔽程序抓取 添加User-Agent头信息以避免403
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
// httpURLConnection.set...更多请求设置
httpURLConnection.connect();
InputStream is = httpURLConnection.getInputStream();
// 不需要设置可以直接下面 也就是org.apache.commons.io.FileUtils中copyURLToFile(URL source, File destination)的写法
// InputStream is=url.openStream();
try {
FileOutputStream fos = new FileOutputStream(file);
try {
// 照搬org.apache.commons.io.IOUtils
// IOUtils.copy(InputStream input, OutputStream output) 开始
byte[] buffer = new byte[1024 * 4];
int n;
while (-1 != (n = is.read(buffer))) {
fos.write(buffer, 0, n);
}
// IOUtils.copy(InputStream input, OutputStream output) 结束
} finally {
try {
if (is != null) {
fos.close();
}
} catch (IOException ioe) {
// ignore
}
}
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException ioe) {
// ignore
}
}
}private void mixedDownload(String urlstr, String path) throws Exception {
mixedDownload(urlstr, new File(path));
}//使用IOUtils减少代码量 弃用FileUtils以对请求进行详细设置 推荐
private void mixedDownload(String urlstr, File file) throws Exception {
URL url = new URL(urlstr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0");
httpURLConnection.setConnectTimeout(10000);
httpURLConnection.setReadTimeout(10000);
httpURLConnection.connect();
InputStream is = httpURLConnection.getInputStream();
try {
FileOutputStream output = FileUtils.openOutputStream(file);
try {
IOUtils.copy(is, output);
} finally {
IOUtils.closeQuietly(output);
}
} finally {
IOUtils.closeQuietly(is);
}
}private void getContentAsString(String urlstr) throws Exception {
URL url = new URL(urlstr);
InputStream is=url.openStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
IOUtils.copy(is, bos);
System.out.println(bos.toString());
}
}
旧瓶新酒-获取网络资源即爬取下载页面内容(图片、html、css、js等)的更多相关文章
- java实现多线程使用多个代理ip的方式爬取网页页面内容
项目的目录结构 核心源码: package cn.edu.zyt.spider; import java.io.BufferedInputStream; import java.io.FileInpu ...
- scrapy(四): 爬取二级页面的内容
scrapy爬取二级页面的内容 1.定义数据结构item.py文件 # -*- coding: utf-8 -*- ''' field: item.py ''' # Define here the m ...
- Scrapy爬取静态页面
Scrapy爬取静态页面 安装Scrapy框架: Scrapy是python下一个非常有用的一个爬虫框架 Pycharm下: 搜索Scrapy库添加进项目即可 终端下: #python2 sudo p ...
- UI自动化之特殊处理四(获取元素属性\爬取页面源码\常用断言)
获取元素属性\爬取页面源码\常用断言,最终目的都是为了验证我们实际结果是否等于预期结果 目录 1.获取元素属性 2.爬取页面源码 3.常用断言 1.获取元素属性 获取title:driver.titl ...
- scrapy模拟浏览器爬取验证码页面
使用selenium模块爬取验证码页面,selenium模块需要另外安装这里不讲环境的配置,我有一篇博客有专门讲ubuntn下安装和配置模拟浏览器的开发 spider的代码 # -*- coding: ...
- [Python_scrapy图片爬取下载]
welcome to myblog Dome地址 爬取某个车站的图片 item.py 中 1.申明item 的fields class PhotoItem(scrapy.Item): # define ...
- 爬取百度页面代码写入到文件+web请求过程解析
一.爬取百度页面代码写入到文件 代码示例: from urllib.request import urlopen #导入urlopen包 url="http://www.baidu.com& ...
- 使用BeautifulSoup自动爬取微信公众号图片
爬取微信分享的图片,根据不同的页面自行修改,使用BeautifulSoup爬取,自行格局HTML修改要爬取图片的位置 import re import time import requests imp ...
- Python爬取 | 唯美女生图片
这里只是代码展示,且复制后不能直接运行,需要配置一些设置才行,具体请查看下方链接介绍: Python爬取 | 唯美女生图片 from selenium import webdriver from fa ...
随机推荐
- SpringBoot——HelloWorld
微服务和单体应用的宏观理解 微服务:一组小型应用通过HTTP的方式进行沟通的开发思想 单体应用:ALL IN ONE 单体应用的不足: 随着业务逻辑的不断更新和迭代开发,起初的小型应用会不断膨胀,当应 ...
- linux常用命令三
linux常用命令三 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 ...
- 实验吧CTF练习题---WEB---貌似有点难解析
实验吧web之貌似有点难 地址:http://www.shiyanbar.com/ctf/32 flag值:SimCTF{daima_shengji} 解题步骤: 1.打开题目页面,观察题目要 ...
- 无法安装64位office,因为您的PC上有32位
场景:安装visio2013时,突然报以下错误 解决方案: 1. 单击开始--所有程序--附件--运行,在运行输入“regedit“ 2. 弹出注册表编辑器窗口,选择HKEY_CLASSES_ROOT ...
- Winform中使用FastReport的DesignReport时怎样设置Table的size自动调整
场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- 重学Java(一):与《Java编程思想》的不解之缘
说起来非常惭愧,我在 2008 年的时候就接触了 Java,但一直到现在(2018 年 10 月 10 日),基础知识依然非常薄弱.用一句话自嘲就是:十年 IT 老兵,Java 菜鸡一枚. 于是,我想 ...
- win下的mongodb安装和基础操作
一.下载地址: https://www.mongodb.com/download-center/community 二.安装错误: 1.安装过程中报错(类似下图): 原因:没有管理员权限 解决:管理员 ...
- 疑难杂症----udf提权无法导出.dll
昨天进行测试一个网站,进行udf提权时候,没办法导出.dll, 起初以为是这个马的问题,后来用专用马,一样不行,但是有报错了,有上网找了半天,终于被我找到了. Mysql数据库从文件导入或导出到文件, ...
- [Linux] CentOS安装GNOME时,fwupdate-efi-12-5.el7.centos.x86_64 conflicts with grub2-common-1:2.02-0.65.el7.centos.noarch
参考文章:https://createdpro.com/a/100006 该问题源于文件的版本冲突: grub2-common包的冲突,所以要将该包使用yum update grub2-commonn ...
- 基于vue实现搜索高亮关键字
有一个需求是在已有列表中搜索关键词,然后在列表中展示含有相关关键字的数据项并且对关键字进行高亮显示,所以该需求需要解决的就两个问题: 1.搜索关键词过滤列表数据 2.每个列表高亮关键字 ps: 此问题 ...