【问题标题】:html parser to search and replace the some values using javahtml解析器使用java搜索和替换一些值
【发布时间】:2011-06-18 04:54:58
【问题描述】:

我正在寻找一个可以搜索和替换锚标记的 html 解析器,例如

ex
<a href="/ima/index.php">example</a>
to
<a href="http://www.example.com/ima/index.php">example</a>

更新:

我的代码用 jsoup 但不工作

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.common.collect.ImmutableList;
import com.google.common.net.InternetDomainName;

public class test {
    public static void main(String args[]) throws IOException {

          Document doc = Jsoup.connect("http://www.google.com").get();

          String html =doc.outerHtml().toString();

         // System.out.println(html);

           Elements links = doc.select("a");



            for (Element link : links) {
             String href=link.attr("href");
             if(href.startsWith("http://"))
             {

             }
             else
             {
                 html.replaceAll(href,"http://www.google.com"+href);
             }
            }
            System.out.println(html);
    }

}

【问题讨论】:

  • 你不能只使用&lt;BASE HREF='http://www.example.com/'&gt; 来达到这个结果吗?或者您是否希望覆盖网站的内容?
  • 你可以做到这一点..抱歉这个愚蠢的问题
  • 不傻,只是过度设计。 :)

标签: java jsoup html-parsing


【解决方案1】:

你可以用 String.replaceAll() 和一个匹配的正则表达式来做到这一点

<a href="/

查找所有相关链接。

html = html.replaceAll("<a href=\"/", "<a href=\"http://www.google.com/\"");

【讨论】:

    【解决方案2】:

    这是一道编程题吗?如果您正在寻找一个预制的 Java 文件或其他类似的东西,那么您来错地方了。如果你想写这样的东西,那么你可以只搜索以a href=/"开头并以/"&gt;结尾的文本实例,然后你可以检查href值,如果它是一个相对路径(即以/开头),你可以在开头添加其他文本。

    【讨论】:

      【解决方案3】:
      package javaapplication4;
      
      import org.jsoup.Jsoup;
      import org.jsoup.nodes.Document;
      import org.jsoup.nodes.Element;
      import org.jsoup.select.Elements;
      
      /**
       *
       * @author derek
       */
      public class Main
      {
          /**
           * @param args the command line arguments
           */
          public static void main(String[] args)
          {
              try
              {
                  Document document = Jsoup.connect("http://www.google.com").get();
                  Elements elements = document.select("a");
      
                  for (Element element : elements)
                  {
                      element.baseUri();
                  }
                  System.out.println(document);
              }
              catch (Exception e)
              {
                  e.printStackTrace(System.err);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        此代码将文档中的相对链接更改为代码使用 jsoup 库的绝对链接

        private void absoluteLinks(Document document, String baseUri)    {
            Elements links = document.select("a[href]");
            for (Element link : links)  {
                if (!link.attr("href").toLowerCase().startsWith("http://"))    {
                    link.attr("href", baseUri+link.attr("href"));
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2019-01-12
          • 2021-04-16
          • 2013-02-07
          • 2011-01-05
          • 1970-01-01
          • 1970-01-01
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多