fix an attribute bug
parent
145628557d
commit
bfadac756a
|
@ -8,6 +8,7 @@ import org.htmlcleaner.DomSerializer;
|
||||||
import org.htmlcleaner.HtmlCleaner;
|
import org.htmlcleaner.HtmlCleaner;
|
||||||
import org.htmlcleaner.TagNode;
|
import org.htmlcleaner.TagNode;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
import javax.xml.namespace.NamespaceContext;
|
import javax.xml.namespace.NamespaceContext;
|
||||||
|
@ -70,7 +71,7 @@ public class Xpath2Selector implements Selector {
|
||||||
|
|
||||||
private XPath2NamespaceContext() {
|
private XPath2NamespaceContext() {
|
||||||
put("fn", NamespaceConstant.FN);
|
put("fn", NamespaceConstant.FN);
|
||||||
put("xslt",NamespaceConstant.XSLT);
|
put("xslt", NamespaceConstant.XSLT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -116,15 +117,20 @@ public class Xpath2Selector implements Selector {
|
||||||
result = xPathExpression.evaluate(document, XPathConstants.STRING);
|
result = xPathExpression.evaluate(document, XPathConstants.STRING);
|
||||||
}
|
}
|
||||||
if (result instanceof NodeList) {
|
if (result instanceof NodeList) {
|
||||||
StreamResult xmlOutput = new StreamResult(new StringWriter());
|
|
||||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
|
||||||
NodeList nodeList = (NodeList) result;
|
NodeList nodeList = (NodeList) result;
|
||||||
if (nodeList.getLength() == 0) {
|
if (nodeList.getLength() == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
transformer.transform(new DOMSource(nodeList.item(0)), xmlOutput);
|
Node item = nodeList.item(0);
|
||||||
return xmlOutput.getWriter().toString();
|
if (item.getNodeType() == Node.ATTRIBUTE_NODE || item.getNodeType() == Node.TEXT_NODE) {
|
||||||
|
return item.getTextContent();
|
||||||
|
} else {
|
||||||
|
StreamResult xmlOutput = new StreamResult(new StringWriter());
|
||||||
|
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||||
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
|
transformer.transform(new DOMSource(item), xmlOutput);
|
||||||
|
return xmlOutput.getWriter().toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result.toString();
|
return result.toString();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -152,9 +158,14 @@ public class Xpath2Selector implements Selector {
|
||||||
StreamResult xmlOutput = new StreamResult();
|
StreamResult xmlOutput = new StreamResult();
|
||||||
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
||||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||||
xmlOutput.setWriter(new StringWriter());
|
Node item = nodeList.item(i);
|
||||||
transformer.transform(new DOMSource(nodeList.item(i)), xmlOutput);
|
if (item.getNodeType() == Node.ATTRIBUTE_NODE || item.getNodeType() == Node.TEXT_NODE) {
|
||||||
results.add(xmlOutput.getWriter().toString());
|
results.add(item.getTextContent());
|
||||||
|
} else {
|
||||||
|
xmlOutput.setWriter(new StringWriter());
|
||||||
|
transformer.transform(new DOMSource(item), xmlOutput);
|
||||||
|
results.add(xmlOutput.getWriter().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
results.add(result.toString());
|
results.add(result.toString());
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package us.codecraft.webmagic.oo;
|
package us.codecraft.webmagic.oo;
|
||||||
|
|
||||||
import us.codecraft.webmagic.Page;
|
import us.codecraft.webmagic.Page;
|
||||||
|
import us.codecraft.webmagic.Site;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -9,7 +10,7 @@ import java.util.List;
|
||||||
* @date: 13-8-1 <br>
|
* @date: 13-8-1 <br>
|
||||||
* Time: 下午10:18 <br>
|
* Time: 下午10:18 <br>
|
||||||
*/
|
*/
|
||||||
@TargetUrl(value="http://my.oschina.net/flashsword/blog/*",sourceRegion = "//div[@class='BlogLinks']")
|
@TargetUrl(value="http://my.oschina.net/flashsword/blog/*",sourceRegion = "//div[@class='BlogLinks']//a/@href")
|
||||||
public class OschinaBlog implements AfterExtractor {
|
public class OschinaBlog implements AfterExtractor {
|
||||||
|
|
||||||
@ExtractBy("//title")
|
@ExtractBy("//title")
|
||||||
|
@ -23,6 +24,13 @@ public class OschinaBlog implements AfterExtractor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterProcess(Page page) {
|
public void afterProcess(Page page) {
|
||||||
content = null;
|
System.out.println("title:\t"+title);
|
||||||
|
System.out.println("content:\t"+content);
|
||||||
|
System.out.println("tags:\t" + tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
OOSpider.create(Site.me().addStartUrl("http://my.oschina.net/flashsword/blog/145796"), OschinaBlog.class)
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1418,7 +1418,7 @@ public class XpathSelectorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testXpath2Selector() {
|
public void testXpath2Selector() {
|
||||||
Xpath2Selector xpath2Selector = new Xpath2Selector("//a");
|
Xpath2Selector xpath2Selector = new Xpath2Selector("//a/@href");
|
||||||
String select = xpath2Selector.select(html);
|
String select = xpath2Selector.select(html);
|
||||||
Assert.assertNotNull(select);
|
Assert.assertNotNull(select);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue