【问题标题】:Getting html of a page in android [duplicate]在android中获取页面的html [重复]
【发布时间】:2012-07-15 15:21:41
【问题描述】:

可能重复:
Is it possible to get the HTML code from WebView

我正在尝试制作一个需要从网站获取 html 代码并通过代码查找图像的应用程序。我现在所在的位置在模拟器中返回了一个 html 代码,但是当我在计算机上打开网站的源代码时,得到的代码不同。

public String getInternetData(String adresse) throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI(adresse);
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while ((l = in.readLine()) !=null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    }finally{
        if (in != null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

我在模拟器中获得的代码以需要启用 JavaScript 和 Cookies 结束。如果这是我的问题,我该如何解决?

任何帮助将不胜感激!

【问题讨论】:

    标签: android html


    【解决方案1】:
    1. 有时问题出在 UserAgent 中(服务器可以发送简化版/移动版页面来查看您的 useragent 字符串)。尝试在您的代码中使用与浏览器相同的用户代理
    2. 尝试使用 HtmlUnit“无头浏览器”框架来执行 javascript。

    【讨论】:

      【解决方案2】:

      请参阅Aymon Fournier 对此SO thread 的回答,

      private String getDownloadButtonOnly(String url){
          HttpGet pageGet = new HttpGet(url);
      
          ResponseHandler<String> handler = new ResponseHandler<String>() {
              public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                  HttpEntity entity = response.getEntity();
                  String html; 
      
                  if (entity != null) {
                      html = EntityUtils.toString(entity);
                      return html;
                  } else {
                      return null;
                  }
              }
          };
      
          pageHTML = null;
          try {
              while (pageHTML==null){
                  pageHTML = client.execute(pageGet, handler);
              }
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      
              Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);
              Matcher matcher = pattern.matcher(pageHTML);
              String displayHTML = null;
              while(matcher.find()){
                  displayHTML = matcher.group();
              }
      
          return displayHTML;
      }
      
          @Override
          public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
              mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
              webview.getSettings().setJavaScriptEnabled(true);
              WebViewClient anchorWebViewClient = new WebViewClient()
              {
      
                  @Override
                  public void onPageStarted(WebView view, String url, Bitmap favicon) {
                      super.onPageStarted(view, url, favicon);
                      String downloadButtonHTML = getDownloadButtonOnly(url);
                      if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){
                          lastLoadedURL = url;
                          webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url);
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 2020-08-13
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        相关资源
        最近更新 更多