如何使用html解析器获取网页标题
如何使用HTML解析器获取给定URL的网页标题?是否可以使用正则表达式来获得标题?我宁愿使用HTML解析器。如何使用html解析器获取网页标题
我在Java Eclipse IDE中工作。
我已经尝试使用下面的代码,但不成功。
任何想法?
提前致谢!
import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.tags.TitleTag;
public class TestHtml {
public static void main(String... args) {
Parser parser = new Parser();
try {
parser.setResource("http://www.yahoo.com/");
NodeList list = parser.parse(null);
Node node = list.elementAt(0);
if (node instanceof TitleTag) {
TitleTag title = (TitleTag) node;
System.out.println(title.getText());
}
} catch (ParserException e) {
e.printStackTrace();
}
}
}
根据你(重新)问题,问题是,你只检查的第一个节点Node node = list.elementAt(0);
,而你应该遍历列表中找到标题(这是不是第一个)。你也可以使用NodeFilter
作为你的parse()
只返回TitleTag
然后标题将在第一个,你不必迭代。
RegEx match open tags except XHTML self-contained tags
聪明的你不想使用正则表达式。
要使用HTML解析器,我们需要知道您正在使用哪种语言。既然你说你在“日食上”,我会假设Java。
查看http://www.ibm.com/developerworks/xml/library/x-domjava/的描述,概述和各种观点。
嗯 - 假设您使用的是java,但在大多数语言中都有相应的功能 - 您可以使用SAX解析器(例如将任何html转换为xhtml的TagSoup)并在您的处理程序中执行:
public class MyHandler extends org.xml.sax.helpers.DefaultHandler {
boolean readTitle = false;
StringBuilder title = new StringBuilder();
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if(localName.equals("title") {
readTitle = true;
}
}
public void endElement(String uri, String localName, String name)
throws SAXException {
if(localName.equals("title") {
readTitle = false;
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(readTitle) title.append(new String(ch, start, length));
}
}
,你用它在你的解析器(与tagsoup为例):
org.ccil.cowan.tagsoup.Parser parser = new Parser();
MyHandler handler = new MyHander();
parser.setContentHandler(handler);
parser.parse(an input stream to your html file);
return handler.title.toString();
我曾与下面的代码尝试了segment.But我仍然无法得到的结果。 公共类TestParser { 公共静态无效的主要(字符串参数... args){ 尝试{ 解析器解析器=新的解析器(); parser.setResource(“http://www.youtube.com”); NodeList list = parser.parse(null); Node node = list.elementAt(0); 如果(节点的instanceof TitleTag){ TitleTag标题=(TitleTag)节点; System.out.println(title.getText()); } } catch(ParserException e){ e.printStackTrace(); } } – smartcode 2010-07-09 08:36:00
你应该把这个放在你的问题中,并且定义你使用哪种语言和哪个库(或者添加相应的标签),如果问题不太模糊,那么得到答案会更有效。 .. – Vinze 2010-07-09 08:45:38
::我编辑了我的问题,如果你可以给任何想法或更正,它会更好的我..thanx! – smartcode 2010-07-09 08:58:51
顺便说一句,已经有一个非常简单的HTMLParser标题提取。可以使用的是:http://htmlparser.sourceforge.net/samples.html
的方法来运行它是(从HTMLParser的代码库中): 执行命令
bin/parser http://website_url TITLE
或运行
java -jar <path to htmlparser.jar> http://website_url TITLE
或从你的代码调用方法
org.htmlparser.Parser.main(String[] args)
与参数new String[] {"<website url>", "TITLE"}
这将是非常容易使用HTMLAgilityPack你只需要得到的HTTPRequest的性反应中字符串的形式。
String response=httpRequest.getResponseString(); // this may have a few changes or no
HtmlDocument doc= new HtmlDocument();
doc.loadHtml(response);
HtmlNode node =doc.DocumentNode.selectSingleNode("//title"); // this line will fetch title tage from whole html document and return collection could iterate
node.innerText; //gives you the title of the page
helloWorld节点。的innerText包含的helloWorld
OR
String response=httpRequest.getResponseString(); // this may have a few changes or no
HtmlDocument doc= new HtmlDocument();
doc.loadHtml(response);
HtmlNode node =doc.DocumentNode.selectSingleNode("//head");// this additional will get head which is a single node in html than get title from head's childrens
HtmlNode node =node.selectSingleNode("//title"); // this line will fetch title tage from whole html document and return collection could iterate
node.innerText; //gives you the title of the page
[你不能解析HTML或XML定期expresisons] [1] [1]:http://stackoverflow.com/questions/ 1732348 /正则表达式匹配开放标签,除了-XHTML-自足标签/ 1732454#1732454 – Glyph 2011-10-16 03:49:55