美文网首页java相关
简单爬虫技术之爬取之--般网页

简单爬虫技术之爬取之--般网页

作者: 行舟2009 | 来源:发表于2017-08-13 21:31 被阅读10次

    背景

    一般互联网公司还有数据分析公司,喜欢使用爬虫爬取页面,并进行数据分析.爬虫的数据种类很多,由于本人仅接触java技术,所以只提供简单的java爬虫技术.感谢网络上那么多的资料分享.现就我自己经验,编写一个java的demo.

    爬取页面

    目前有很多网站做了爬虫反扒技术,只有一些服务器比较牛的公司(暂且这么说吧),经得起爬虫爬取页面,像新浪微博的页面,爬虫技术爬取就比较困难了,现在以容易爬取的页面为例,使用GET方式,爬取路径:

    http://top.baidu.com/buzz?b=259&c=9&fr=topbuzz_b612_c9(百度的历史人物数据,这个比较方便挑选关键数据,比如人物名称,人物简介,相关贴吧和相关视频,本文仅获取内容并保存到文件.)另外百度搜索还支持榜单的嵌入定制,很方便的,嵌入条件如下图.

    Demo代码

    Get请求类

    package yuxiSoftware.cn.demo;

    import java.io.IOException;

    import org.apache.http.HttpEntity;

    import org.apache.http.ParseException;

    import org.apache.http.client.ClientProtocolException;

    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;

    /**

    * Get请求获取页面内容

    * @author yuxiSoftware

    */

    public class HttpRequestBean {

    public String getMethod(String uri) {

    // 创建默认httpclient实例

    CloseableHttpClient httpClient = HttpClients.createDefault();

    try {

    // 创建httpGet

    HttpGet httpGet = new HttpGet(uri);

    // 执行get请求

    CloseableHttpResponse response = httpClient.execute(httpGet);

    try {

    // 获取响应实体

    HttpEntity entity = response.getEntity();

    // 获取响应状态

    System.out.println(response.getStatusLine());

    if (entity != null) {

    // 响应内容

    return EntityUtils.toString(entity, "gb2312");

    }

    } catch (ParseException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } finally {

    response.close();

    }

    } catch (ClientProtocolException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return "";

    }

    }

    爬取内容输出类

    package yuxiSoftware.cn.demo;

    import java.io.File;

    import java.io.FileNotFoundException;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStream;

    /**

    * 文件输出类

    * @author yuxiSoftware

    *

    */

    public class OutPutToFile {

    public void writeFile(String s,String url){

    File f = new File(url);

    OutputStream out = null;

    try {

    out = new FileOutputStream(f);

    } catch (FileNotFoundException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    byte b [] = s.getBytes();

    try {

    out.write(b);

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    try {

    out.close();

    } catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    }

    }

    main方法类

    package yuxiSoftware.cn.demo;

    /**

    * 爬虫小demo

    * @author yuxiSoftware

    *

    */

    public class WormDemo {

    public static void main(String[] args) {

    String url = "http://top.baidu.com/buzz?b=259&c=9&fr=topbuzz_b612_c9";

    OutPutToFile outPutToFile = new OutPutToFile();

    HttpRequestBean httpRequestBean = new HttpRequestBean();

    String s = httpRequestBean.getMethod(url);

    String fileurl = "C:\\Users\\yuxiSoftware\\Desktop\\百度爬取页面.txt";

    outPutToFile.writeFile(s, fileurl);

    }

    }

    输出结果

    说明

    该java技术,仅供大家学习娱乐使用,非商业用途,如有不妥之处,请联系作者删除,谢谢!.

    相关文章

      网友评论

        本文标题:简单爬虫技术之爬取之--般网页

        本文链接:https://www.haomeiwen.com/subject/pbwerxtx.html