抓取网页信息并获取生成xml文件(以网页彩票数据为例)
来源:互联网 发布:andrew marc牌子知乎 编辑:程序博客网 时间:2024/04/30 08:47
一、网页抓取
使用httpclient抓取 传入网页url
public static String clientTest(String url){
@SuppressWarnings("deprecation")
HttpClient hc=new DefaultHttpClient();
HttpGet get=new HttpGet(url);
String backContent="";
try {
HttpResponse response=hc.execute(get);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream is = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
//end 读取整个页面内容
backContent = buffer.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return backContent;
}
二、截取所需部分
传入获取的网页信息,截取开始的的字符串和结束的字符串(如:"<tbody id=\"cpdata\">","</tbody>")
public static String sub(String str,String beginstr,String endstr){
int b=str.indexOf(beginstr);
int e=str.indexOf(endstr);
int le=endstr.length();
String result=str.substring(b, e+le);
return result;
}
int b=str.indexOf(beginstr);
int e=str.indexOf(endstr);
int le=endstr.length();
String result=str.substring(b, e+le);
return result;
}
三、替换掉不符合xml规则的字符串
例如: data-foldGroup=1 在xml里面算错误的
str=str.replaceAll("data-foldGroup=1", " ");
四、最后就是保存了
将处理符合xml规则的字符串保存为path这个文件下(path文件完整路径)
public static void saveFile(String str,String path){
File file=new File(path);
PrintWriter pfp=null;
try {
pfp= new PrintWriter(file);
pfp.print(str);
pfp.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
pfp.close();
}
}
File file=new File(path);
PrintWriter pfp=null;
try {
pfp= new PrintWriter(file);
pfp.print(str);
pfp.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
pfp.close();
}
}
整个调用过程
public static void main(String[] args) {
String url="http://trend.caipiao.163.com/ssq/?beginPeriod=2015001&endPeriod=2015118";
String str=clientTest(url);
//System.out.println(str);
str=sub(str,"<tbody id=\"cpdata\">","</tbody>");
str=str.replaceAll("data-foldGroup=1", " ");
str=str.replaceAll("data-foldColor=ball_red", " ");
str=str.replaceAll("data-award=1", " ");
str=str.replaceAll("data-foldColor=ball_blue", " ");
System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+str);
String path="D:/java/hello.xml";
saveFile(str,path);
}
String url="http://trend.caipiao.163.com/ssq/?beginPeriod=2015001&endPeriod=2015118";
String str=clientTest(url);
//System.out.println(str);
str=sub(str,"<tbody id=\"cpdata\">","</tbody>");
str=str.replaceAll("data-foldGroup=1", " ");
str=str.replaceAll("data-foldColor=ball_red", " ");
str=str.replaceAll("data-award=1", " ");
str=str.replaceAll("data-foldColor=ball_blue", " ");
System.out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+str);
String path="D:/java/hello.xml";
saveFile(str,path);
}
0 0
- 抓取网页信息并获取生成xml文件(以网页彩票数据为例)
- java网页数据抓取源代码(抓取电话和身份证信息为例)
- python网络爬虫抓取ajax动态网页数据:以抓取KFC门店地址为例
- HttpClient+Jsoup 抓取网页信息(网易贵金属为例)
- C#: 抓取网页类(获取网页中所有信息)
- C# 抓取网页类(获取网页中所有信息)
- 使用python抓取网页(以人人网新鲜事和团购网信息为例)
- 使用python抓取网页(以人人网新鲜事和团购网信息为例)
- 网页数据抓取并分析
- 抓取网页数据并解析
- scrapy 抓取ajax请求的网页-以ifanr为例
- Python抓取网页数据 生成 iOS plist 文件
- java 网页爬虫(以扒取amazon网页信息为例)
- asp在服务器端获取网页生成的xml文件,并解析
- Python抓取网页并保存为PDF
- 抓取网页数据并解析Android
- 用正则表达式抓取制定网页的特定内容(本文以抓取a标签为例)
- 如何抓取Js动态生成数据且以滚动页面方式分页的网页
- vc 网络连接的测试方法
- oracle按时间段查询
- MATLAB中的分类器
- iOS 被键盘遮挡时,带有textfield的tableview自动上移
- 请问两个div之间的上下距离怎么设置
- 抓取网页信息并获取生成xml文件(以网页彩票数据为例)
- android中actionBar中字体颜色设置
- LintCode:最小路径和
- 【IOS 开发学习总结-OC-39】★★ios开发之 MVC 模式
- webrtc sip html5
- ZOJ 3778 Talented Chef(计算器模拟)
- kafka深度解析
- docker搭建简单web服务
- MFC消除视图闪烁