亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 4205 | 回復(fù): 1
打印 上一主題 下一主題

HttpClient根據(jù)jsoup解析網(wǎng)頁 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-03-19 17:05 |只看該作者 |倒序?yàn)g覽
HttpClient根據(jù)jsoup解析網(wǎng)頁







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代碼進(jìn)行解析  
  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為“l(fā)eft”的div里面 id為“recommend”的div里面ul里面li里面a標(biāo)簽   
  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.     * 根據(jù)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();// 創(chuàng)建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.                // 獲得相應(yīng)實(shí)體   
  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 + "】出現(xiàn)異常!");   
  60. 60.            e.printStackTrace();   
  61. 61.        } finally {   
  62. 62.            httpClient.getConnectionManager().shutdown();   
  63. 63.        }   
  64. 64.        return html;   
  65. 65.    }   
  66. 66.}  
復(fù)制代碼

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2012-03-19 17:05 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP