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条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址