mas_china 2013-04-29 23:28 采纳率: 0%
浏览 222
已采纳

用java对固定网址进行解析,获取页面部分内容。

比如说我通过地址获取到了网页的html文件,我现在想获取<span class="value" id="sku-discount-price" itemprop="price">6.89</span>  标签之间的6.89这个值,用java该怎么写呢?怎么做才是最合理的,我自己也尝试的写了一点,请各位高手们指教。如果有更好的方案,欢迎分享一下。

<div class="inf-pnl-price-detail">   
   <dl>   
   <dt>Price:</dt>   
   <dd>   
   <div class="price price-highlight">   
   <del class="original-price">US $    
   <span class="" id="sku-price">7.66</span>   
   <span class="separator">/</span>   
   <span class="unit">piece</span>   
   </del>   
   </div>   
   </dd>   
   <dt>Discount Price:</dt>   
   <dd>   
   <div class="price price-highlight">   
   <span class="currency" itemprop="priceCurrency" content="USD">US $</span>   
   <span class="value" id="sku-discount-price" itemprop="price">6.89</span>   
   <span class="separator">/</span><span class="unit"> piece </span>   
   <span class="time-left">(7  days left )</span>   
   </div>   
   </dd>   
   </dl>   
   </div>   

 我自己尝试写的代码:

public class TestUrl {

    public static void main(String[] args) {
        Long l1 = System.currentTimeMillis();
        
        String string = "http://www.aliexpress.com/item/10pcs-lot-New-arrival-Hot-sale-fashion-hoomia-jonadab-magicpencil-magic-pencil-earphones-in-earfree-shipping/848760252.html";
        String str3 = "";
        String str[] = new String[750];
        String str2 = "";
        int i = 0;
        try {
            URL readSource = new URL(string);
            BufferedReader input = new BufferedReader(new InputStreamReader(readSource.openStream()));
            input.skip(15555);
            
            
            while((str2 = input.readLine()) !=null){
                    str[i] = str2;
                    i++;
            }
            str3 = str[1]+str[2]+str[3]+str[4]+str[5]+str[6]+str[7];
            System.out.println("1====================>"+str3);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        String tempStr2 = str3.replaceAll(".*itemprop=\"price\">", "");
        String tempStr3 = tempStr2.replaceAll("</span>.*", "");
        System.out.println("tempStr2:"+tempStr3);
        
        Long l2 = System.currentTimeMillis();
        System.out.println("time:"+(l2-l1));
    }
}
  • 写回答

2条回答 默认 最新

  • jinnianshilongnian 2013-04-30 07:13
    关注

    直接使用jsoup css选择器语法 进行选择

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?