【问题标题】:parse html page解析html页面
【发布时间】:2012-01-06 21:10:38
【问题描述】:

我想解析 1 个 url 和后记,我想从中访问一些数据。

try {
        Document doc = Jsoup.connect("http://abc.com/en/currency/default.aspx").get();//abc is for example as i cant put site name
        Elements td = doc.select("ctl00_ContentPlaceHolder1_currencylist_rptCurrencyList_ctl01_trList"); //this is the name of table row in html page i will show html page snippet also
        String temp=td.val();
        info.setText(temp);
    } 
     catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

我要解析的html页面的sn-p如下

       <tr id="ctl00_ContentPlaceHolder1_currencylist_rptCurrencyList_ctl01_trList">
<td width="400px" class="CurrencyListItems">             
         UK POUND
         </td>
<td width="60px;" class="CurrencyListItemsIN" align="center">
         5.72
         </td>
<td width="150px;" class="CurrencyListItemsLast">
           <table cellspacing ="0" cellpadding ="0" width="100%">
                   <tr>
                     <td class="CurrencyListBANKNOTES" align="center">                         
                     5.625
                     </td>
                     <td class="CurrencyListBANKNOTES2" width="75px" align="center">

                     5.75
                     </td>
                   </tr>
            </table>
         </td>

我想从上面 html 英镑 ,5.625,5.75 我尝试了上面的代码,但问题是它没有解析 URL,只有在尝试时才会出现

【问题讨论】:

    标签: android html-parsing


    【解决方案1】:

    试试这个:

    Element tr = doc.getElementById("ctl00_ContentPlaceHolder1_currencylist_rptCurrencyList_ctl01_trList");
    

    试试

    String contents = tr.text().trim();
    contents = contents.replaceAll("\\s+"," "); 
    contents = contents. replaceAll("\\<.*?>","-");
    String []values = contents.split("-");
    

    Elements elements = tr.select("*");
    for (Element element : elements) {
        System.out.println(element.ownText());
    }
    

    【讨论】:

    • @Nammari-ur 较早的解决方案完美运行,只是 thng 是 tat split dnt wrkd 所以里面的值数组整个 4 个项目来了,但是这不是一个概率,这个支持是为了 tat ans.thnx v much..
    • @Nammari-thi 新更新的答案你输入它是 gvng errot on for (Element element : elements) as elements not found..
    • @ Nammari-hey thnx alot..它工作了我 gt wat 是 mmitake 而不是 for 循环中的元素,它应该是 allElementInTr..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2013-02-28
    • 1970-01-01
    • 2011-07-06
    • 2012-09-20
    • 2010-12-03
    相关资源
    最近更新 更多