Records of Webview development experience in those years

Get the real link of the website video

Go straight to tools

You can call it directly. This tool class will return all the links in the loaded web page. As for how to deal with the links, it is your own business. I have personally tested all the videos I have encountered so far, and it can capture their links.


import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/3/22.
 */

public class ParseWebUrlHelper {
    private static ParseWebUrlHelper parseWebUrlHelper;
    private String webUrl;
    private Activity mAct;
    private WebView webView;
    private int timeOut = 20 * 1000;
    private OnParseWebUrlListener onParseListener;

    public static ParseWebUrlHelper getInstance() {
        if (parseWebUrlHelper == null) parseWebUrlHelper = new ParseWebUrlHelper();
        return parseWebUrlHelper;
    }

    public ParseWebUrlHelper init(Activity act, String url) {
        this.mAct = act;
        this.webUrl = url;
        ViewGroup mainView = (ViewGroup) mAct.findViewById(android.R.id.content);
        this.webView = new WebView(mAct);
        this.webView.setLayoutParams(new LinearLayout.LayoutParams(1, 1));
        mainView.addView(this.webView);
        initWebSettings();
        return this;
    }

    private void initWebSettings() {
        WebView mWebView = this.webView;
        mWebView.clearFocus();
        WebSettings mWebSettings = mWebView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setDefaultTextEncodingName("utf-8");
        mWebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        mWebSettings.setPluginState(WebSettings.PluginState.ON);
        mWebSettings.setDisplayZoomControls(false);
        mWebSettings.setUseWideViewPort(true);
        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setAllowContentAccess(true);
        mWebSettings.setSupportZoom(true);
        mWebSettings.setAllowContentAccess(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setBuiltInZoomControls(true);// 隐藏缩放按钮
        mWebSettings.setUseWideViewPort(true);// 可任意比例缩放
        mWebSettings.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。
        mWebSettings.setSavePassword(true);
        mWebSettings.setSaveFormData(true);// 保存表单数据
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setTextZoom(100);
        mWebSettings.setDomStorageEnabled(true);
        mWebSettings.setSupportMultipleWindows(true);// 新加//我就是没有这一行,死活不出来。MD,硬是没有人写这一句!
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            mWebSettings.setMediaPlaybackRequiresUserGesture(true);
        }
        if (Build.VERSION.SDK_INT >= 16) {
            mWebSettings.setAllowFileAccessFromFileURLs(true);
            mWebSettings.setAllowUniversalAccessFromFileURLs(true);
        }
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setLoadsImagesAutomatically(true);
        mWebSettings.setAppCacheEnabled(true);
        mWebSettings.setAppCachePath(mAct.getCacheDir().getAbsolutePath());
        mWebSettings.setDatabaseEnabled(true);
        mWebSettings.setGeolocationDatabasePath(mAct.getDir("database", 0).getPath());
        mWebSettings.setGeolocationEnabled(true);
        CookieManager instance = CookieManager.getInstance();
        if (Build.VERSION.SDK_INT < 21) {
            CookieSyncManager.createInstance(mAct.getApplicationContext());
        }
        instance.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT >= 21) {
            instance.setAcceptThirdPartyCookies(mWebView, true);
        }
        mWebView.setWebViewClient(new MyWebViewClient());
        enabledCookie(webView);//启用cookie
    }
    public ParseWebUrlHelper setLoadUrl(String url){
        this.webUrl=url;
        return this;
    }
    public ParseWebUrlHelper startParse(){
        webView.loadUrl(this.webUrl);
        return this;
    }
    /*启用cookie*/
    private void enabledCookie(WebView web) {
        CookieManager instance = CookieManager.getInstance();
        if (Build.VERSION.SDK_INT < 21) {
            CookieSyncManager.createInstance(mAct);
        }
        instance.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT >= 21) {
            instance.setAcceptThirdPartyCookies(web, true);
        }
    }

    public ParseWebUrlHelper setOnParseListener(OnParseWebUrlListener onParseListener) {
        this.onParseListener = onParseListener;
        return this;
    }

    private class MyWebViewClient extends WebViewClient {

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if(request.getUrl().toString().startsWith("intent")||request.getUrl().toString().startsWith("youku")){
                return true;
            }else{
                return super.shouldOverrideUrlLoading(view, request);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("intent")||url.startsWith("youku")){
                return true;
            }else{
                return super.shouldOverrideUrlLoading(view, url);
            }
        }

        /*解决ssl证书问题*/
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            onParseListener.onFindUrl(url);
            return super.shouldInterceptRequest(view, url);
        }

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                String url = request.getUrl().toString();
                onParseListener.onFindUrl(url);
            }
            return super.shouldInterceptRequest(view, request);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO: Implement this method
            super.onPageStarted(view, url, favicon);
            startConut();//加载超时处理
        }

    }
    /*解决webview加载超时问题*/
   private void startConut(){
        final Timer timer=new Timer();
        TimerTask timerTask=new TimerTask(){
            @Override
            public void run()
            {
                onParseListener.onError("解析视频超时,请检查网速或网络是否出现问题...");
                timer.cancel();
                timer.purge();
            }
        };
        timer.schedule(timerTask,timeOut,1);
    }

    public interface OnParseWebUrlListener {
        void onFindUrl(String url);
        void onError(String errorMsg);
    }
}

transfer

//初始化
this.parseWebUrlHelper=ParseWebUrlHelper.getInstance().init(this.mAct,"");
//解析网页中视频
parseWebUrlHelper.setOnParseListener(new ParseWebUrlHelper.OnParseWebUrlListener() {
                    @Override
                    public void onFindUrl(String url) {
                        Log.d("webUrl",url);
                        //*****处理代码
                    }
                    @Override
                    public void onError(String errorMsg) {
                    //****出错监听
                    }

question

Q: Some videos in webview cannot be loaded.
A: http://blog.csdn.net/u013148839/article/details/65448474
Q: Webview intercepts requests
A: https://segmentfault.com/a/1190000006805635
Q: webview Due to the certificate problem, the webpage cannot be loaded normally:
A:http://blog.csdn.net/sd19871122/article/details/54632199

Summary by others

1.http://www.cnblogs.com/lee0oo0/p/4026774.html

Get the complete source code of webview [including iframe source code]

main problem

dynamic loading

iframe nesting

0. The window is also a frame
1. Get the frame
2. Determine whether the url of the frame has been searched
3. Determine whether the frame is the same domain name
4. Directly search for the same domain name
5. Directly load the url of the frame separately from different domain names, as a new 6. Search
until a video tag is found

Problems detected when iframe links are opened separately

secondary issue

Remove Chrome anti-debugging

Unlimited breakpoint anti-debugging

Reference: https://blog.csdn.net/zzgzzg00/article/details/79504827

end

It’s so easy to go astray when it comes to programming. My most fundamental purpose is to obtain website video links, and then I found that the source code of some websites was not fully obtained, and then the purpose quietly changed to obtain the source code, and then tossed for a few days. I still haven’t solved the problem of obtaining the source code, and then I suddenly remembered that I have studied the same problem before, and I also wrote an article on obtaining video links. This is the real wheel-making. I have already studied it once. The problem has obviously been encountered once, why did it fall into the pit again this time? And I have been in the pit for a few days, but I didn’t realize it. Oh, stupid, I don’t really have a long memory. It was only at the end that I found out that the purpose of my obtaining the source code was to obtain the video link. In other words, this article I wrote before. The function: Web Reverse: Obtaining the real link of the website video through the WebView in Android

reference

1. Break through the iframe siege
2. Python crawler's solution to multi-layer nested iframe
3. Crawler acquisition: solve the problem of dynamic loading data and frame frame
4. Java crawler advanced - use of ip pool, iframe nesting, asynchronous access cracking

Guess you like

Origin blog.csdn.net/qq_26914291/article/details/132224749
Recommended