获取网页指定元素和内容

一、利用jsoup抓取网页,并获得指定dom元素

jsoup jar  下载地址 https://jsoup.org/download

 

 
  1. try {

  2. Document doc = null;

  3. doc = Jsoup.connect("http://www.163.com/xxx.html").get();

  4.  
  5. // dom解析获得指定元素

  6. Element mainArea = doc.getElementById("mainArea");

  7. Elements datas = mainArea.getElementsByAttribute("data-period");

  8.  
  9. // 遍历Elements datas,获取指定属性

  10. for(Element data:datas){

  11. String win_number = data.attr("data-win-number");

  12. String period = data.attr("data-period");

  13. }

  14. } catch (IOException e) {

  15. System.out.println("以上地址未获取到页面");

  16. e.printStackTrace();

  17. }


 

 

二、利用HttpURLConnection获取ajax返回json数据

 
  1. try {

  2. // json请求地址

  3. String urlStr = "xxxxxx";

  4.  
  5. // 创建连接

  6. URL url = new URL(urlStr);// 请求地址

  7. HttpURLConnection connection = (HttpURLConnection) url.openConnection();

  8. connection.setDoOutput(true);

  9. connection.setDoInput(true);

  10. connection.setRequestMethod("GET");// 这里是请求方式 ,或者"POST"

  11. connection.setUseCaches(false);

  12. connection.setInstanceFollowRedirects(false);

  13. // content-Type要根据目标接口的类型填,常用就"form"

  14. // 百度网站自身防盗链,直接发起get请求没有结果,抓取真实请求参数

  15. connection.setRequestProperty("Referer", "http://www.baidu.com/XXXXXXXXXX");

  16. connection.connect();

  17.  
  18. // 读取响应

  19. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

  20. String ss = null;

  21. String total = "";

  22.  
  23. // 输出响应结果。校验你是否操作成功

  24. while ((ss = reader.readLine()) != null) {

  25. total += ss;

  26. }

  27. System.out.println("total=" + total);

  28.  
  29. // 解析响应结果:将json String 转换为JSONObject

  30. JSONObject rootJsonObj = JSONObject.fromObject(total);

  31.  
  32. // 解析JSONObject,如下两种get方式

  33. JSONObject data = rootJsonObj.getJSONObject("data");//同(JSONObject) data.get("data")

  34. JSONArray list = data.getJSONArray("list"); //同(JSONArray) data.get("list")

  35.  
  36. // 断开连接

  37. reader.close();

  38. connection.disconnect();

  39. } catch (Exception e) {

  40. e.printStackTrace();

  41. }

在Android的WebView中,可以通过以下代码获取指定class的元素内容: ```java WebView webView = findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { // 获取 class="example" 的元素内容 webView.loadUrl("javascript:window.android.onElementContentLoaded(document.getElementsByClassName('example')[0].innerHTML)"); } }); // 添加 JavaScript 接口 webView.addJavascriptInterface(new Object() { @JavascriptInterface public void onElementContentLoaded(String content) { // 处理获取到的元素内容 Log.d("WebView", "Element content: " + content); } }, "android"); ``` 在这个例子中,我们首先设置了WebViewClient的onPageFinished方法,当网页加载完成后会自动调用此方法。在这个方法中,我们通过JavaScript代码获取指定class的元素内容,并通过WebView的loadUrl方法将获取到的内容回传给Android应用程序。 最后,我们通过addJavascriptInterface方法将一个Java对象添加为JavaScript接口,这个接口中包含了onElementContentLoaded方法,用于处理获取到的元素内容。在JavaScript代码中,我们通过window.android.onElementContentLoaded方法将获取到的元素内容传递给这个接口方法。 需要注意的是,这种方式可能会受到网页结构和JavaScript代码的影响,需要根据实际情况进行调整。同时,这种方式也可能会涉及到隐私和安全问题,请谨慎使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值