你能提供解析HTML的例子吗? [英] Can you provide examples of parsing HTML?

查看:84
本文介绍了你能提供解析HTML的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用各种语言和解析库解析HTML?




回答时:

个人评论将与以回答关于如何用正则表达式解析HTML的问题,以此来展示正确的做事方式。



为了保持一致性,我要求例子是解析定位标记中 href 的HTML文件。为了方便搜索这个问题,我要求你遵循这种格式。

语言:[语言名称]



<图书馆:[图书馆名称]

  [示例代码] 

请使图书馆成为图书馆文件的链接。如果你想提供一个除解压缩链接以外的例子,请包括:



目的:[解析是什么]

解决方案

语言: JavaScript
b $ b Library: jQuery

  $。each($('a [href]'),function(){
console.debug(this.href);
});

(使用firebug console.debug输出...)

并加载任何html页面:

  $ .get('http://stackoverflow.com/' ,function(page){
$(page).find('a [href]')。each(function(){
console.debug(this.href);
}) ;
});

使用这个函数的另一个函数,我认为在链接方法时它更干净。 b $ b

How do you parse HTML with a variety of languages and parsing libraries?


When answering:

Individual comments will be linked to in answers to questions about how to parse HTML with regexes as a way of showing the right way to do things.

For the sake of consistency, I ask that the example be parsing an HTML file for the href in anchor tags. To make it easy to search this question, I ask that you follow this format

Language: [language name]

Library: [library name]

[example code]

Please make the library a link to the documentation for the library. If you want to provide an example other than extracting links, please also include:

Purpose: [what the parse does]

解决方案

Language: JavaScript
Library: jQuery

$.each($('a[href]'), function(){
    console.debug(this.href);
});

(using firebug console.debug for output...)

And loading any html page:

$.get('http://stackoverflow.com/', function(page){
     $(page).find('a[href]').each(function(){
        console.debug(this.href);
    });
});

Used another each function for this one, I think it's cleaner when chaining methods.

这篇关于你能提供解析HTML的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆