<div id="apDiv6">
<table width="100%" height="182" cellspacing="3" border="0">
<tbody><tr>
<td valign="middle" height="147" align="center"><a href="#"><img width="211" height="140" src="../images/group/pic04.jpg" /></a></td>
</tr>
<tr>
<td align="left"><a style=" color:#213964"><strong>需要取的内容</strong></a></td>
</tr>
</tbody></table>
</div>
------解决思路----------------------
把html作为xml文档来看待,用dom4j通过XPath可以轻易找到指定内容
------解决思路----------------------
先通过javascript获取其中的值,再通过链接让值作为参数发给服务端接收
------解决思路----------------------
$.each($("#apDiv6 > table > tbody >tr "),function(i){
if($(this).attr("align")=="left"){
alert($(this).text())
}
})
------解决思路----------------------
使用dom4j 、xpath 。依赖jar文件 dom4j-1.6.1.jar jaxen-1.1.1.jar
package com.zf.test;
import java.io.StringReader;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XPATH {
public static void main(String[] args) throws Exception {
String str = "<div id=\"apDiv6\">"+
"<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+
"<tbody><tr>"+
"<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+
"</tr>"+
"<tr>"+
"<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+
"</tr>"+
"</tbody></table>"+
"</div>";
SAXReader reader = new SAXReader();
Document doc = reader.read(new StringReader(str));
Node node = doc.selectSingleNode("//div/table/tbody/tr/td/a/strong");
System.out.println(node.getText());
}
}
方式二: 使用jsoup通过jquery格式搜索html中dom元素 依赖jar文件 jsoup-1.7.1
package com.zf.test;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JSOUP {
public static void main(String[] args) {
String str = "<div id=\"apDiv6\">"+
"<table width=\"100%\" height=\"182\" cellspacing=\"3\" border=\"0\">"+
"<tbody><tr>"+
"<td valign=\"middle\" height=\"147\" align=\"center\"><a href=\"#\"><img width=\"211\" height=\"140\" src=\"../images/group/pic04.jpg\" /></a></td>"+
"</tr>"+
"<tr>"+
"<td align=\"left\"><a style=\" color:#213964\"><strong>需要取的内容</strong></a></td>"+
"</tr>"+
"</tbody></table>"+
"</div>";
Document doc = Jsoup.parse(str);
Elements es = doc.select("a strong");
for (Element element : es) {
System.out.println(element.html());
}
}
}