7

我如何用 Java 解析从 Google Chrome 和 Mozilla Firefox 导出的书签文件。是否有任何库可用于直接解析它们并获取其中的 URL。

用 Java 解析它们的示例代码也很受欢迎。

4

4 回答 4

7

In most cases, you don't really need to parse the HTML file. Chrome stores its bookmarks in a JSON file. It's a lot simpler to just read that file using a JSON parser.

The file you are interested in is located at (on Linux, anyway, Google around for other O/S):

/home/your_name/.config/google-chrome/Default/Bookmarks

JSON parsing is easy. Google around or start with How to parse JSON in Java.

If you want to visualize JSON data before you start digging through it, then also have a look at http://chris.photobooks.com/json/default.htm.

于 2013-02-27T07:20:40.640 回答
4

根据发布的新评论,解决方案是使用 JSOUP 开源程序来执行此操作。JSOUP 仅接受 HTTP 或 HTTPS 协议,因此您可能希望在本地服务器(如 tomcat)上托管导出的书签 HTML 并获取它的 DOM

 http://yourip:<port>/<yourProject>/<bookmark.html>. 

JSOUP 是不言自明的。

其他更简单的方法:

Chrome 和 Firefox 书签以 JSON 格式存储,如下所示。

Java方式:我建议你使用JSON来解析这些。根据以下结构创建一个引用 Java 对象。

或者简单地使用 UNIX 命令提示符并执行

 grep -i "url" <bookmark file path> | cut -d":" -f2

但是,如果您仍然对 Chrome API 感兴趣,请访问:http: //developer.chrome.com/extensions/bookmarks.html

{
   "checksum": "702d8e600a3d70beccfc78e82ca7caba",
   "roots": {
  "bookmark_bar": {
     "children": [ {
        "date_added": "12939920104154671",
        "id": "3",
        "name": "Development/Tutorials/Git/git-svn - KDE TechBase",
        "type": "url",
        "url": "http://techbase.kde.org/Development/Tutorials/Git/git-svn"
     }, {
        "date_added": "12939995405838705",
        "id": "4",
        "name": "QJson - Usage",
        "type": "url",
        "url": "http://qjson.sourceforge.net/usage.html"
于 2013-02-22T05:19:00.483 回答
0

我对这个问题有点晚了。但如果它仍然相关:我需要做同样的事情(以及其他书签来源:GitHub Stars、Netscape 和 Google Bookmarks)并构建我自己的。您可以从我的仓库中查看并获取它:https ://github.com/IvoLimmen/mystart 。

于 2018-10-19T17:44:05.463 回答
0

如果有人有兴趣:这是一个如何处理解析 Chrome 书签 JSON 文件的 scala 片段(虽然没有经过彻底测试,只是为了得到这个想法):

import org.json4s.DefaultFormats
import org.json4s.native.JsonMethods
import org.junit.Test

class BookmarksImporterTest {

  implicit val formats: DefaultFormats.type = DefaultFormats

  def analyse(element: Node): List[Node] = {
   element.children.flatMap(c => {
      c.`type` match {
        case Some("folder") => c.children.flatMap(r => analyse(r))
        case Some("url")    => List(c)
        case _              => println("???"); List()
      }
    })
  }

  @Test
  def test(): Unit = {
    val source    = scala.io.Source.fromFile("bookmarks.json")
    val json      = JsonMethods.parse(source.reader())
    val bookmarks = json.extract[ChromeBookmarks]

    val bms = bookmarks.roots.flatMap {
      case (name, elements) => analyse(elements)
    }
    println("found " + bms.size + " entries")
  }

}

case class ChromeBookmarks(checksum: String, roots: Map[String, Node], version: Int)

case class Node(
    id: Option[String],
    name: Option[String],
    url: Option[String],
    children: List[Node],
    `date-added`: Option[Long],
    `date-modified`: Option[Long],
    `type`: Option[String]
)
于 2019-03-26T16:38:02.000 回答