close

瓦瑞文翻譯


b.限定屬性資料開首相同:[class^=value]、tr[class^=value]

2.搜尋特定屬性下的資料限定:
[class=value]、[class^=value]、[class$=value]、[class*=value]、tr[class=value]、tr[class^=value]、tr[class$=value]、tr[class*=value]

介紹JSOUP

1.搜索特定屬性:

在XML有許多含有命名空間的Tag,如以下Tag並做出範例

Elements title = doc.select("[class]");


以下為省略大部分資料的Tag,並做出典範

<tr class="rowGroup top bottom">.....</tr>



c.限制屬性資料結尾不異:[class$=value]、tr[class$=value]

前面說的都是抓取夾在Tag間的文字,這邊教一下抓取Tag內src或是href屬性內的網址,使用的是attr()
package com.example.jsoup;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
    URL url;
    TextView t01;
    Thread th;
    String te01,te02,te03;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t01=(TextView) this.findViewById(R.id.t01);    //保持TextView
        th=new Thread(r0);                //履行緒
        th.start();                    //讓執行緒入手下手工作
    }
    private Runnable r0=new Runnable(){
        public void run(){
            try {
                url=new URL("http://sports.williamhill.com/bet/zh-hk/results///E/8386900/thisDate/2015/10/27/9:00:00//-England-%E5%B0%8D-%E7%83%8F%E5%85%8B%E8%98%AD.html");
                Document doc =  Jsoup.parse(url, 3000);        //保持該網址
                Elements title = doc.select("tr[class]");    //抓取為tr且有class屬性的所有Tag
                for(int i=0;i<title.size();i++){            //用FOR個體抓取選定的Tag內容
                    Elements title_select=title.get(i).select("td");//選擇第i個後選取所有為td的Tag
                    te01=title_select.get(0).text();        //只抓取第 0,2,3 Tag的文字
                    te02=title_select.get(2).text();
                    te03=title_select.get(3).text();
                    runOnUiThread(new Runnable() {             //將內容交給UI執行緒做顯示
                            public void run(){   
                                t01.append("
"+te01+"
");
                                t01.append(te02+"
");
                                t01.append(te03+"
");
                            }     
                        });
                    Thread.sleep(100);    //避免履行緒跑太快而UI履行續顯示太慢,覆蓋掉te01~03內容所以設個延遲,也可以使用AsyncTask-異步使命
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }
    };
}


JSOUP是個可以解析HTML和XML的套件,因為HTML和XML有所謂的Tag,就是<title></title>這個左右巨細於括起來的器材
它能獲得指定的Tag,一直到下一個</     >結束為止,所以凡是有Tag的網頁,JSOUP都能獲得Tag包夾的文字

<div id="contentCenter" >.....</div>
Elements title = doc.select("div#contentCenter");    //抓取Tag為div且有id屬性並限定資料為"contentCenter"的Tag


以下典範是抓取這6段Tag內容

d.限制屬性資料裡含有某段文字:[class*=value]、tr[class*=value]

以下先使用一個範例來示範
抓取的是網頁中心的欄位了局翻譯社可右鍵>搜檢元素 查看Tag:
http://sports.williamhill.com/bet/zh-hk/results///...



<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<script type="text/javascript" src="http://whdn.williamhill.com/core/ob/static/cust/js/minified/main_racing.js?ver=723fa5ea119ca4e349d4e10120810d9a"></script>
Document doc =  Jsoup.parse(url, 3000);        //連結該網址
Elements title = doc.select("script[type=text/javascript]");
String te=title.attr("src");            //用來獲取src屬性的值
以上大部份都是取自於參考資料翻譯社在其他網頁上測試後的實作典範榜樣翻譯社如今後有常常利用再補上不足的地方

參考資料:
http://a6350202.pixnet.net/blog/post/148400470-%E3...



<tr class="rowGroup top bottom">.....</tr>


以上一個體例可以知道抓取特定屬性其實抓到的Tag還是很多,所以必需再縮小局限,就有了屬性下的資料限制
一樣是以方才的Tag
<div id="contentA">
    .....
    <table    .....>
        .....
        <tbody>
            .....
            <tr class="rowGroup top bottom">
                <td style="padding-top: 0pt;" class="borderBottom" rowspan="1">賽事投注</td>
                <td></td>
                <td>烏克蘭 (2.75)  </td>
                <td style="padding-top: 0pt; text-align: center" class="borderBottom" rowspan="1">Y</td>
            </tr>
            <tr class="rowGroup top bottom">.....</tr>
            <tr class="rowGroup top bottom">.....</tr>
            <tr class="rowGroup top bottom">.....</tr>
            <tr class="rowGroup top bottom">.....</tr>
            <tr class="rowGroup top bottom">.....</tr>
            .....
        </tbody>
        .....
    </table>
    .......
</div>
//抓取Tag為div且有id屬性並限制資料為"contentA",且底下有<table>Tag翻譯社在底下又有<tbody>Tag翻譯社在底下又有<tr>Tag且還要具有class屬性的所有Tag
Elements title = doc.select("div#contentA>table>tbody>tr[class]");


所以我們的程式碼就必需寫:

<dc:subject>臺北市今明天氣預告</dc:subject>
Elements title = doc.select("dc|subject");    //抓取Tag為<dc:subject>


文章標籤
jsoup android android jsoup jsoup教學 jsoup規範 jsoup select JSOUP抓取網頁文字 JSOUP解析
以方才的網頁為範例天成翻譯公司們就必須搜索這行Tag,而他就有個class屬性
3.限制id屬性:
#id、div#id
利用事前先至JSOUP官網將JAR檔載下來並匯入lib

Elements title = doc.select("tr[class$=bottom]");    //抓取Tag為tr且有class屬性並限制資料結尾為"bottom"的所有Tag


有的時刻利用select()時若是只拔取某種Tag,拔取到的Tag會相當多翻譯社這時候候就需要利用條件搜索翻譯社以下是利用正規表示式的方式做搜尋

Elements title = doc.select("tr[class^=rowGroup]");    //抓取Tag為tr且有class屬性並限制資料開頭為"rowGroup"的所有Tag


    <tr class="rowGroup top bottom">
        <td style="padding-top: 0pt;" class="borderBottom" rowspan="1">賽事投注</td>
        <td></td>
        <td>烏克蘭 (2.75)  </td>
        <td style="padding-top: 0pt; text-align: center" class="borderBottom" rowspan="1">Y</td>
    </tr>
    <tr class="rowGroup top bottom">.....</tr>
    <tr class="rowGroup top bottom">.....</tr>
    <tr class="rowGroup top bottom">.....</tr>
    <tr class="rowGroup top bottom">.....</tr>
    <tr class="rowGroup top bottom">.....</tr>

Android規範:

Elements title = doc.select("tr[class*=top]");        //抓取Tag為tr且有class屬性並限定資料含有"top"這段文字的所有Tag


以上的Tag是沒有id的,所以挑了一個有id屬性的Tag

記住在AndroidManifest.xml文件添加權限:


[class]、tr[class]

Elements title = doc.select("tr[class=rowGroup top bottom]");    //抓取Tag為tr且有class屬性並限定資料為"rowGroup top bottom"的所有Tag

<uses-permission android:name="android.permission.INTERNET"/>
Document doc =  Jsoup.parse(url翻譯社 3000);            //貫穿連接該網址
Elements title = doc.select("tr[class]");        //抓取Tag為tr且有class屬性的所有Tag



Android利用JSOUP抓取網頁資料
如許就可以抓取到<tr>這種Tag,而且要具有class屬性的所有Tag
或是直接利用[class],如許能抓到的範圍就比力大,是所有具有class屬性的Tag

a.限制屬性資料完全相同:[class=value]、tr[class=value]


5.含有定名空間的Tag
ns|tag
以上為比較經常使用的正規默示式搜索法,如需要更多具體的等以後有利用到再補上
=====================================================================================================


4.限制父標籤下的子標籤:
div>table




以下內文出自: http://xxs4129.pixnet.net/blog/post/165417214-android%e4%bd%bf%e7%94%a8jsoup%e6%8a%93%e5%8f%96%e7%b6有關各國語文翻譯公證的問題歡迎諮詢天成翻譯公司02-77260931
arrow
arrow
    文章標籤
    翻譯社
    全站熱搜
    創作者介紹
    創作者 marien4f30x 的頭像
    marien4f30x

    marien4f30x@outlook.com

    marien4f30x 發表在 痞客邦 留言(0) 人氣()