抓取网站数据解析的工作,其中,使用到GET和POST方法获取html数据。

使用GET方式:

  1. /**
  2. * 使用get方式获取html数据
  3. *
  4. * @param strURL(需要访问的网站)
  5. * @return
  6. * @throws Exception
  7. */
  8. public String getHTML(String strURL) throws Exception {
  9. //创建浏览器
  10. HttpClient httpClient = HttpClients.createDefault();
  11. String html = null;
  12. //预防网址链接中包含特殊字符,将url转为uri
  13. URL url = new URL(strURL);
  14. URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
  15. url.getQuery(), null);
  16. //使用get方式
  17. HttpGet request = new HttpGet(uri);
  18. HttpResponse response;
  19. try {
  20. //连接网址获取返回的数据
  21. response = httpClient.execute(request);
  22. //将返回的数据按照gbk的方式编码
  23. html = EntityUtils.toString(response.getEntity(), "GBK");
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. //断开连接
  28. request.abort();
  29. //返回网址所发挥的html数据
  30. return html;
  31. }

使用该方法便可以获取得到网站所发挥的html数据。

使用POST方式:

  1. /**
  2. * 使用post方式获取html数据
  3. *
  4. * @param libraryUrl(需要访问的网站)
  5. * @param params(需要传入的参数)
  6. * @return
  7. * @throws Exception
  8. */
  9. public String postHTML(String strURL, List<NameValuePair> params)
  10. throws Exception {
  11. //创建浏览器
  12. HttpClient httpClient = HttpClients.createDefault();
  13. String html = null;
  14. //预防网址链接中包含特殊字符,将url转为uri
  15. URL url = new URL(strURL);
  16. URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
  17. url.getQuery(), null);
  18. //使用POST方式
  19. HttpPost request = new HttpPost(uri);
  20. //将参数封装进UrlEncodedFormEntity中
  21. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
  22. request.setEntity(entity);
  23. HttpResponse response;
  24. try {
  25. //连接网址获取返回的数据
  26. response = httpClient.execute(request);
  27. //将返回的数据按照gbk的方式编码
  28. html = EntityUtils.toString(response.getEntity(), "GBK");
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. //断开连接
  33. request.abort();
  34. //返回网址所发挥的html数据
  35. return html;
  36. }

其中,参数params的封装可以参照以下方式:

  1. List<NameValuePair> params = new ArrayList<NameValuePair>();
  2. //以键值对的方式存储
  3. params.add(new BasicNameValuePair("format", "hitcount"));

使用GET与POST方式获取html数据的更多相关文章

  1. Http方式获取网络数据

    通过以下代码可以根据网址获取网页的html数据,安卓中获取网络数据的时候会用到,而且会用Java中的sax方式解析获取到数据.(sax解析主要是解析xml)具体代码如下: package com.wy ...

  2. 基于uFUN开发板的心率计(一)DMA方式获取传感器数据

    前言 从3月8号收到板子,到今天算起来,uFUN到手也有两周的时间了,最近利用下班后的时间,做了个心率计,从单片机程序到上位机开发,到现在为止完成的差不多了,实现很简单,uFUN开发板外加一个Puls ...

  3. IOS开发之Post 方式获取服务器数据

    //1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...

  4. [NSURLSession/Delegate]用Post方式获取网络数据并把数据显示到表格

    #pragma mark 实现NSURLSessionDataDelegate代理 @interface ViewController ()<UITableViewDataSource,UITa ...

  5. [NSURLConnection]分别用Post和Get方式获取网络数据并把数据显示到表格

    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UIButton* getButton; ...

  6. 用 get 同步/异步 方式获取网络数据并输出

    //同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...

  7. jQuery AJAX获取JSON数据解析多种方式示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...

  9. android—获取网络数据

    取网络数据主要靠发交易(或者说请求,接口等),而这些交易由java中的网络通信,HttpURLConnection和HttpClient实现,以下是具体例子.   大家都知道,网络通信,发送请求有两种 ...

随机推荐

  1. SPOJ 913 Query on a tree II

    spoj题面 Time limit 433 ms //spoj的时限都那么奇怪 Memory limit 1572864 kB //1.5个G,疯了 Code length Limit 15000 B ...

  2. sh_08_打印分隔线

    sh_08_打印分隔线 def print_line(char, times): print(char * times) print_line("hi ", 40)

  3. Vue CLI4.0版本正式发布了!一起来看看有哪些新的变化吧

    Vue CLI4.0版本正式发布 这个主要的版本更新主要关注底层工具的必要版本更新.更好的默认设置和其他长期维护所需的微调. 我们希望为大多数用户提供平稳的迁移体验. Vue CLI v4提供了对Ni ...

  4. Java网络编程之Netty服务端ChannelOption.SO_BACKLOG配置

    ChannelOption.SO_BACKLOG对应的是tcp/ip协议listen函数中的backlog参数,函数listen(int socketfd,int backlog)用来初始化服务端可连 ...

  5. Docker push image to Docker hub

    1. Before push image to Docker Hub, register an account in https://hub.docker.com/ 2.Input "doc ...

  6. [LeetCode]-DataBase-Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...

  7. js运行原理

    https://www.youtube.com/watch?v=8aGhZQkoFbQ

  8. ES6 暂时性死区

    在ES6中,声明变量新增了两个关键字:let命令和const命令 如果在区块中存在let或者const命令时,任何变量都必须在声明之前使用,无论是区块外部的全局变量或者是区块内部的变量: /* 区块外 ...

  9. hook C++

    Intercepting Calls to COM Interfaces(hook com接口) 通过COM组件IFileOperation越权复制文件 代码注入之远程线程篇 使用VC++通过远程进程 ...

  10. Text Classification

    Text Classification For purpose of word embedding extrinsic evaluation, especially downstream task. ...