我开始使用xpath表达式
我有以下网址:
http://www.newark.com/white-rodgers/586-902/contactor-spst-no-12vdc-200a-bracket/dp/35M1913?MER=PPSO_N_P_EverywhereElse_None
它保存html页面内容,使用以下xpath在javascript:中生成相同的ul元素
//*[@id="moreStock_5257711"]
//*[@id="priceWrap"]/div[1]/div/a/following-sibling::ul
//html/body/div/div/div/div/div/div/div/div/div/div/a/following-sibling::ul
使用这个xpath,sholud如何在java中获得相同的ul元素
我尝试过使用"html清洁器",它在xpath-中失败了
"//*[@id="priceWrap"]/div[1]/div/a/following-sibling::ul",
"//html/body/div/div/div/div/div/div/div/div/div/div/a/following-sibling::ul"
它为"//*[@id='moreStock_5257711']"这个xpath工作。所以下面的代码,我已经尝试在html清洁
package com.test.htmlcleaner.HtmlCleaner;
import java.io.IOException;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
public static void main(String[] args) {
try {
HtmlCleaner htmCleaner = new HtmlCleaner();
CleanerProperties cleanerProperties = htmCleaner.getProperties();
cleanerProperties.setTranslateSpecialEntities(true);
cleanerProperties.setTransResCharsToNCR(true);
cleanerProperties.setOmitComments(true);
String s = "http://www.newark.com/white-rodgers/586-902/contactor-spst-no-12vdc-200a-bracket/dp/35M1913?MER=PPSO_N_P_EverywhereElse_None";
Document doc = Jsoup.connect(s).timeout(30000).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2").get();
String pageContent=doc.toString();
TagNode node = htmCleaner.clean(pageContent);
Object[] statsNode = node.evaluateXPath("//*[@id='moreStock_5257711']");
if(statsNode.length > 0) {
for(int i=0;i<statsNode.length;i++){
TagNode resultNode = (TagNode)statsNode[i];
System.out.println("hi");
System.out.println("Element Text : " +resultNode.getText().toString().trim());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XPatherException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我要求所有的xpaths-sholud在java中使用一个包
有人能建议我使用java来处理获取ul元素的所有xpath表达式吗。
谢谢你的问候。
尝试调试HtmlCleaner创建的实际HTML DOM树。使用以下代码:
String pageContent = doc.toString();
TagNode node = htmCleaner.clean(pageContent);
StringWriter buffer = new StringWriter();
node.serialize(new PrettyHtmlSerializer(cleanerProperties), buffer);
System.out.println(buffer.toSting());
现在,尝试在这个缓冲区输出上应用所有XPath,看看它们为什么不起作用。