`
diddyrock
  • 浏览: 45296 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论
阅读更多
xml 的属性必须有属性值,且属性必须用引号括起来
在xml中可以不考虑使用属性,完全使用元素
<![CDTA[包含特殊字符恶不需要解释为标记]]>

分析与处理html其实还好,java中的sax与dom实现可以很好的完成对标准html文件的解析
所以首先第一步就是利用第三方的能够容错的处理程序把需要分析的html程序先进行纠错分析,比如用neko的documentFragment,然后转化之后再进行处理分析

HTML <base> 标签
定义和用法
base 元素可规定页面中所有链接的基准 URL。
通常情况下,浏览器会从当前文档的 URL 中提取相应的元素来填写相对 URL 中的空白。
使用 <base> 标签可以改变这一点。浏览器随后将不再使用当前文档的 URL,而使用指定的基本 URL 来解析所有的相对 URL。这其中包括 <a>、<img>、<link>、<form> 标签中的 URL。

解析器如果要解析外链,必须首先解析出baseTag的值,这一点很重要
Q: How does a link change?
A: Any link that a user can create on your site automatically gets a new "nofollow" attribute. So if a blog spammer previously added a comment like
Visit my <a href="http://www.example.com/">discount pharmaceuticals</a> site.
That comment would be transformed to
Visit my <a href="http://www.example.com/" rel="nofollow">discount pharmaceuticals</a> site.
rel="nofollow"可以帮助爬虫避开spam,所以这里需要看是否有此属性
methord="post"说明此链接是write形式,无需爬行

linkParams.clear();
    linkParams.put("a", new LinkParams("a", "href", 1));
    linkParams.put("area", new LinkParams("area", "href", 0));
    if (conf.getBoolean("parser.html.form.use_action", false)) {
      linkParams.put("form", new LinkParams("form", "action", 1));
    }
    linkParams.put("frame", new LinkParams("frame", "src", 0));
    linkParams.put("iframe", new LinkParams("iframe", "src", 0));
    linkParams.put("script", new LinkParams("script", "src", 0));
    linkParams.put("link", new LinkParams("link", "href", 0));
    linkParams.put("img", new LinkParams("img", "src", 0));

只有以上几种类型包含有链接,其余的类型就不加处理

for (int i= 0; i < attrs.getLength(); i++ ) {
            Node attr = attrs.item(i);
            String attrName = attr.getNodeName();
            if (params.attrName.equalsIgnoreCase(attrName)) {
              target = attr.getNodeValue();
            } else if ("rel".equalsIgnoreCase(attrName) &&
                       "nofollow".equalsIgnoreCase(attr.getNodeValue())) {
              noFollow = true;
            } else if ("method".equalsIgnoreCase(attrName) &&
                       "post".equalsIgnoreCase(attr.getNodeValue())) {
              post = true;
            }
          }
          if (target != null && !noFollow && !post)
            try {
             
              URL url = (base.toString().indexOf(';') > 0) ?
                fixEmbeddedParams(base, target) :  new URL(base, target);
              outlinks.add(new Outlink(url.toString(),
                                       linkText.toString().trim(), conf));
            } catch (MalformedURLException e) {
              // don't care
            }所谓的target就是指src=,href=,action,等等
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics