免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4161 | 回复: 1
打印 上一主题 下一主题

HttpClient根据jsoup解析网页 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 17:05 |只看该作者 |倒序浏览
HttpClient根据jsoup解析网页







Java代码
  1. 1.package jsoup;   
  2. 2.  
  3. 3.import org.apache.http.HttpEntity;   
  4. 4.import org.apache.http.HttpResponse;   
  5. 5.import org.apache.http.HttpStatus;   
  6. 6.import org.apache.http.client.HttpClient;   
  7. 7.import org.apache.http.client.methods.HttpGet;   
  8. 8.import org.apache.http.impl.client.DefaultHttpClient;   
  9. 9.import org.apache.http.util.EntityUtils;   
  10. 10.import org.jsoup.Jsoup;   
  11. 11.import org.jsoup.nodes.Document;   
  12. 12.import org.jsoup.nodes.Element;   
  13. 13.import org.jsoup.select.Elements;   
  14. 14.  
  15. 15./**  
  16. 16. * 利用HttpClient获取html代码,然后使用jsoup对html代码进行解析  
  17. 17. * @author Administrator  
  18. 18. *  
  19. 19. */  
  20. 20.public class JustTest {   
  21. 21.    public static void main(String[] args) {   
  22. 22.        String html = getHtmlByUrl("http://www.iteye.com/");   
  23. 23.        if (html != null && !"".equals(html)) {   
  24. 24.            Document doc = Jsoup.parse(html);   
  25. 25.            Elements linksElements = doc   
  26. 26.                    .select("div#page>div#content>div#main>div.left>div#recommend>ul>li>a");   
  27. 27.            // 以上代码的意思是 找id为“page”的div里面 id为“content”的div里面 id为“main”的div里面   
  28. 28.            // class为“left”的div里面 id为“recommend”的div里面ul里面li里面a标签   
  29. 29.            for (Element ele : linksElements) {   
  30. 30.                String href = ele.attr("href");   
  31. 31.                String title = ele.text();   
  32. 32.                System.out.println(href + "," + title);   
  33. 33.            }   
  34. 34.        }   
  35. 35.    }   
  36. 36.  
  37. 37.    /**  
  38. 38.     * 根据URL获得所有的html信息  
  39. 39.     *   
  40. 40.     * @param url  
  41. 41.     * @return  
  42. 42.     */  
  43. 43.    public static String getHtmlByUrl(String url) {   
  44. 44.        String html = null;   
  45. 45.        HttpClient httpClient = new DefaultHttpClient();// 创建httpClient对象   
  46. 46.        HttpGet httpget = new HttpGet(url);// 以get方式请求该URL   
  47. 47.        try {   
  48. 48.            HttpResponse responce = httpClient.execute(httpget);// 得到responce对象   
  49. 49.            int resStatu = responce.getStatusLine().getStatusCode();// 返回码   
  50. 50.            if (resStatu == HttpStatus.SC_OK) {// 200正常 其他就不对   
  51. 51.                // 获得相应实体   
  52. 52.                HttpEntity entity = responce.getEntity();   
  53. 53.                if (entity != null) {   
  54. 54.                    html = EntityUtils.toString(entity);// 获得html源代码   
  55. 55.                    System.out.println(html);   
  56. 56.                }   
  57. 57.            }   
  58. 58.        } catch (Exception e) {   
  59. 59.            System.out.println("访问【" + url + "】出现异常!");   
  60. 60.            e.printStackTrace();   
  61. 61.        } finally {   
  62. 62.            httpClient.getConnectionManager().shutdown();   
  63. 63.        }   
  64. 64.        return html;   
  65. 65.    }   
  66. 66.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-19 17:05 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP