jsoup reptiles + android (java) uses detailed (entry) + solve incurable diseases

Because the company's business needs, it is doing android developed online tools reptiles waiting to see for a long time, behind the chosen jsoup, ask why you choose it? Do not want to ask? Well I tell you, because of the simple ah. Well, the topic began.

        The first step: go to the official website to download jsoup.jar package https://jsoup.org/ , you can also choose to download here I support what bloggers integral xxxx

       Step two: because okhttputils network with bloggers request tool (Zhang Hong Yang Great God works), if you use the other their own free. download link:

       Step two: two jar package into libs directory android project inside, and then I remember the two of them added to the dependent library in the project right jar package, and then click add as library,

      The third step: to find a requested address it and see the effect, and then continue to give you explain the usage, that it first with Baidu heaven.

 This is a request code

 OkHttpUtils.get()
                .url("https://www.baidu.com")
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int i) {
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        Document doc = Jsoup.parse(s);
                        Elements el=doc.select("div[id=u1]");
                        String map=doc.select("a[name=tj_trmap]").text();
                        Log.d(TAG, "onResponse: "+map);
                        for (Element li : el) {
                            String xw=li.select("a[name=tj_trnews]").text();
                            String all=li.select("a[class=mnav]").text();
                            Log.d(TAG, "onResponse: "+xw);
                            Log.d(TAG, "onResponse: "+all);
                        }
                    }
                });

This is a log of print

This is Baidu code

You can see from above, with three ways to climb these data, the first is a map, and the second is news, is the third row of content.

The role of each line of code:

Document doc = Jsoup.parse (s); // This is the beginning of reptiles, s is returned html code throughout the site.

Two options can be done later, 1 is a single field take enough. 2 is take a list of each piece of data.

        1.String map=doc.select("a[name=tj_trmap]").text();

         select which parameters you need to take is to label field, as:

 May correspond successively to [] brackets is the property of the label. So you take it to the site in the name attribute of a tag is to all fields of tj_trmap.

       2. The pick list for each piece of data, themselves Baidu.

 

Incurable diseases:

  Climb out of the site's html code and the code actually see on the page is not the same.

1.    有时候发现部分看的见,部分看不见,那么你需要看看网站是不是有iframe标签,有的话里面的内容可能真的看不到,还必须重新请求一下iframe里面的地址。

2.请求的地址也有问题,比如说https://stockpage.10jqka.com.cn/realHead_v2.html#hs_000625

网页上打开正常显示数据,而自己请求又是一回事了,因为#后面的东西是不带的,服务器是不认#后面的内容,除非用webview打开,听我师傅说必须拿到json数据才行,后面发现很多网站都这样,那只有自己想解决方法。

解决办法如下:

先地址http://stockpage.10jqka.com.cn/realHead_v2.html#hs_000625,然后用google浏览器打开,打开开发者工具选择Sources,发现last.js返回的就是json数据

然后就是把它拼装成一个链接https://d.10jqka.com.cn/v2/realhead/hs_002540/last.js。值得注意的是它是https,不然会报错。然后数据就拿到了。然后对其进行json数据解析,数据都拿到了,然后在分配到自己app里面。

发布了29 篇原创文章 · 获赞 44 · 访问量 5万+

Guess you like

Origin blog.csdn.net/ai1362425349/article/details/103201094