【发布时间】:2011-07-08 17:00:17
【问题描述】:
我想抓取仅 html 页面,所以当我在此代码中更改正则表达式时.. 它仍在抓取一些 xml 页面.. 任何建议为什么会发生..
public class MyCrawler extends WebCrawler {
Pattern filters = Pattern.compile("(.(html))");
public MyCrawler() {
}
public boolean shouldVisit(WebURL url) {
String href = url.getURL().toLowerCase();
if (filters.matcher(href).matches()) {
return false;
}
if (href.startsWith("http://www.somehost.com/")) {
return true;
}
return false;
}
public void visit(Page page) {
int docid = page.getWebURL().getDocid();
String url = page.getWebURL().getURL();
String text = page.getText();
List<WebURL> links = page.getURLs();
int parentDocid = page.getWebURL().getParentDocid();
System.out.println("Docid: " + docid);
System.out.println("URL: " + url);
System.out.println("Text length: " + text.length());
System.out.println("Number of links: " + links.size());
System.out.println("Docid of parent page: " + parentDocid);
System.out.println("=============");
}
}
【问题讨论】:
-
@Lucero:这应该是一个答案。
-
@Lucero,感谢您的回复.. 是的,感谢您指出 xhtml,我想对 html 和 xhtml 都这样做.. 那么在这种情况下我们如何分析内容类型所以它只抓取 html 和 xhtml..
-
@Jim,感觉不像是一个真正的答案。 ;)
标签: java regex web-crawler