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

terrence_tian 发布于 2013/04/29 23:12
阅读 702
收藏 0

用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>

比如:我想从这段文字里节选出“

<span class="value" id="sku-discount-price" itemprop="price">6.89</span>
”之间的“6.89”这个字符,大家有没有什么好的方案?


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));
	}
}

下面附上我自己写的代码:


加载中
0
terrence_tian
terrence_tian

引用来自“Jeky”的答案

你的回答帮我解决了问题,而且方案比我的好,虽然性能上差不多,但我那是死办法。
返回顶部
顶部