java网络爬虫
2016-04-12 14:56
459 查看
import java.io.*;
import java.net.*;
import java.util.regex.*;
public class Calculator {
static String SendGet(String url) {
String result = “”;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.connect();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),”UTF-8”));
String line;
while ((line = in.readLine()) != null) {
}
} catch (Exception e) {
System.out.println(“exception!” + e);
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
static String RegexString(String targetStr, String patternStr) {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(targetStr);
if (matcher.find()) {
return matcher.group(1);
}
return “Nothing”;
}
public static void main(String[] args) {
String url = “http://www.zhihu.com/explore/recommendations“;
String result = SendGet(url);
String imgSrc = RegexString(result, “question_link.+?>(.+?)<”);
System.out.println(imgSrc);
}
}
http://www.jb51.net/article/57197.htm
import java.net.*;
import java.util.regex.*;
public class Calculator {
static String SendGet(String url) {
String result = “”;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection connection = realUrl.openConnection();
connection.connect();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream(),”UTF-8”));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(“exception!” + e);
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
static String RegexString(String targetStr, String patternStr) {
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(targetStr);
if (matcher.find()) {
return matcher.group(1);
}
return “Nothing”;
}
public static void main(String[] args) {
String url = “http://www.zhihu.com/explore/recommendations“;
String result = SendGet(url);
String imgSrc = RegexString(result, “question_link.+?>(.+?)<”);
System.out.println(imgSrc);
}
}
http://www.jb51.net/article/57197.htm
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- Scrapy的架构介绍
- 爬虫笔记
- 基于C#实现网页爬虫
- Nodejs爬虫进阶教程之异步并发控制
- PHP+HTML+JavaScript+Css实现简单爬虫开发
- 如何优雅地使用c语言编写爬虫
- PHP实现简单爬虫的方法
- NodeJS制作爬虫全过程(续)
- php实现简单爬虫的开发
- node.js基础模块http、网页分析工具cherrio实现爬虫
- PHP爬虫之百万级别知乎用户数据爬取与分析
- 一个PHP实现的轻量级简单爬虫
- nodejs爬虫抓取数据乱码问题总结
- 基于Node.js的强大爬虫 能直接发布抓取的文章哦
- nodeJs爬虫获取数据简单实现代码
- nodejs爬虫抓取数据之编码问题
- Node.js编写爬虫的基本思路及抓取百度图片的实例分享
- python实现爬虫统计学校BBS男女比例(一)
- 使用Python编写基于DHT协议的BT资源爬虫