使用 html5-parser 和 xmls Common Lisp 浏览网页
Navigating a webpage using html5-parser and xmls Common Lisp
我正在尝试获取标题为 "Name" 的列下的第一行,例如对于 https://en.wikipedia.org/wiki/List_of_the_heaviest_people 我想 return 名称 "Jon Brower Minnoch"。到目前为止我的代码如下,但我认为必须有一种更通用的获取名称的方法:
(defun find-tag (tag doc)
(when (listp doc)
(when (string= (xmls:node-name doc) tag)
(return-from find-tag doc))
(loop for child in (xmls:node-children doc)
for find = (find-tag tag child)
when find do (return-from find-tag find)))
nil)
(defun parse-list-website (url)
(second (second (second (third (find-tag "td" (html5-parser:parse-html5 (drakma:http-request url) :dom :xmls)))))))
然后调用函数:
(parse-list-website "https://en.wikipedia.org/wiki/List_of_the_heaviest_people")
我对 xmls 不是很好,不知道如何在某个列下获取 td header。
html5-parser:parse-html5
返回的文档中的元素的形式为:
("name" (attribute-alist) &rest children)
您可以使用标准列表操作函数访问这些部分,但xmls
还提供函数node-name
、node-attrs
和node-children
来访问这三个部分。使用它们会更清楚一些。 编辑:还有函数 xmlrep-attrib-value
获取属性值和 xmlrep-tagmatch
匹配标签名称。 子项要么是纯字符串,要么是元素格式相同。
因此,例如,具有 2x2 table 的 html 文档将如下所示:
(defparameter *doc*
'("html" ()
("head" ()
("title" ()
"Some title"))
("body" ()
("table" (("class" "some-class"))
("tr" (("class" "odd"))
("td" () "Some string")
("td" () "Another string"))
("tr" (("class" "even"))
("td" () "Third string")
("td" () "Fourth string"))))))
为了遍历 dom-tree,让我们像这样定义一个递归深度优先搜索(注意 if-let
依赖于 alexandria
库(要么导入它, 或将其更改为 alexandria:if-let
)):
(defun find-tag (predicate doc &optional path)
(when (funcall predicate doc path)
(return-from find-tag doc))
(when (listp doc)
(let ((path (cons doc path)))
(dolist (child (xmls:node-children doc))
(if-let ((find (find-tag predicate child path)))
(return-from find-tag find))))))
它是用谓词函数和文档调用的。谓词函数被调用时带有两个参数;被匹配的元素及其祖先的列表。为了找到第一个 <td>
,您可以这样做:
(find-tag (lambda (el path)
(declare (ignore path))
(and (listp el)
(xmls:xmlrep-tagmatch "td" el)))
*doc*)
; => ("td" NIL "Some string")
或者找到偶数行的第一个<td>
:
(find-tag (lambda (el path)
(and (listp el)
(xmls:xmlrep-tagmatch "td" el)
(string= (xmls:xmlrep-attrib-value "class" (first path))
"even")))
*doc*)
; => ("td" NIL "Third string")
获得偶数行的第二个 <td>
需要这样的东西:
(let ((matches 0))
(find-tag (lambda (el path)
(when (and (listp el)
(xmls:xmlrep-tagmatch "td" el)
(string= (xmls:xmlrep-attrib-value "class" (first path))
"even"))
(incf matches))
(= matches 2))
*doc*))
您可以定义一个辅助函数来查找第 n 个标签:
(defun find-nth-tag (n tag doc)
(let ((matches 0))
(find-tag (lambda (el path)
(declare (ignore path))
(when (and (listp el)
(xmls:xmlrep-tagmatch tag el))
(incf matches))
(= matches n))
doc)))
(find-nth-tag 2 "td" *doc*) ; => ("td" NIL "Another string")
(find-nth-tag 4 "td" *doc*) ; => ("td" NIL "Fourth string")
您可能想要一个简单的助手来获取节点的文本:
(defun node-text (el)
(if (listp el)
(first (xmls:node-children el))
el))
您可以定义类似的助手来完成您在应用程序中需要做的任何事情。使用这些,您给出的示例将如下所示:
(defparameter *doc*
(html5-parser:parse-html5
(drakma:http-request "https://en.wikipedia.org/wiki/List_of_the_heaviest_people")
:dom :xmls))
(node-text (find-nth-tag 1 "a" (find-nth-tag 1 "td" *doc*)))
; => "Jon Brower Minnoch"
我正在尝试获取标题为 "Name" 的列下的第一行,例如对于 https://en.wikipedia.org/wiki/List_of_the_heaviest_people 我想 return 名称 "Jon Brower Minnoch"。到目前为止我的代码如下,但我认为必须有一种更通用的获取名称的方法:
(defun find-tag (tag doc)
(when (listp doc)
(when (string= (xmls:node-name doc) tag)
(return-from find-tag doc))
(loop for child in (xmls:node-children doc)
for find = (find-tag tag child)
when find do (return-from find-tag find)))
nil)
(defun parse-list-website (url)
(second (second (second (third (find-tag "td" (html5-parser:parse-html5 (drakma:http-request url) :dom :xmls)))))))
然后调用函数:
(parse-list-website "https://en.wikipedia.org/wiki/List_of_the_heaviest_people")
我对 xmls 不是很好,不知道如何在某个列下获取 td header。
html5-parser:parse-html5
返回的文档中的元素的形式为:
("name" (attribute-alist) &rest children)
您可以使用标准列表操作函数访问这些部分,但xmls
还提供函数node-name
、node-attrs
和node-children
来访问这三个部分。使用它们会更清楚一些。 编辑:还有函数 xmlrep-attrib-value
获取属性值和 xmlrep-tagmatch
匹配标签名称。 子项要么是纯字符串,要么是元素格式相同。
因此,例如,具有 2x2 table 的 html 文档将如下所示:
(defparameter *doc*
'("html" ()
("head" ()
("title" ()
"Some title"))
("body" ()
("table" (("class" "some-class"))
("tr" (("class" "odd"))
("td" () "Some string")
("td" () "Another string"))
("tr" (("class" "even"))
("td" () "Third string")
("td" () "Fourth string"))))))
为了遍历 dom-tree,让我们像这样定义一个递归深度优先搜索(注意 if-let
依赖于 alexandria
库(要么导入它, 或将其更改为 alexandria:if-let
)):
(defun find-tag (predicate doc &optional path)
(when (funcall predicate doc path)
(return-from find-tag doc))
(when (listp doc)
(let ((path (cons doc path)))
(dolist (child (xmls:node-children doc))
(if-let ((find (find-tag predicate child path)))
(return-from find-tag find))))))
它是用谓词函数和文档调用的。谓词函数被调用时带有两个参数;被匹配的元素及其祖先的列表。为了找到第一个 <td>
,您可以这样做:
(find-tag (lambda (el path)
(declare (ignore path))
(and (listp el)
(xmls:xmlrep-tagmatch "td" el)))
*doc*)
; => ("td" NIL "Some string")
或者找到偶数行的第一个<td>
:
(find-tag (lambda (el path)
(and (listp el)
(xmls:xmlrep-tagmatch "td" el)
(string= (xmls:xmlrep-attrib-value "class" (first path))
"even")))
*doc*)
; => ("td" NIL "Third string")
获得偶数行的第二个 <td>
需要这样的东西:
(let ((matches 0))
(find-tag (lambda (el path)
(when (and (listp el)
(xmls:xmlrep-tagmatch "td" el)
(string= (xmls:xmlrep-attrib-value "class" (first path))
"even"))
(incf matches))
(= matches 2))
*doc*))
您可以定义一个辅助函数来查找第 n 个标签:
(defun find-nth-tag (n tag doc)
(let ((matches 0))
(find-tag (lambda (el path)
(declare (ignore path))
(when (and (listp el)
(xmls:xmlrep-tagmatch tag el))
(incf matches))
(= matches n))
doc)))
(find-nth-tag 2 "td" *doc*) ; => ("td" NIL "Another string")
(find-nth-tag 4 "td" *doc*) ; => ("td" NIL "Fourth string")
您可能想要一个简单的助手来获取节点的文本:
(defun node-text (el)
(if (listp el)
(first (xmls:node-children el))
el))
您可以定义类似的助手来完成您在应用程序中需要做的任何事情。使用这些,您给出的示例将如下所示:
(defparameter *doc*
(html5-parser:parse-html5
(drakma:http-request "https://en.wikipedia.org/wiki/List_of_the_heaviest_people")
:dom :xmls))
(node-text (find-nth-tag 1 "a" (find-nth-tag 1 "td" *doc*)))
; => "Jon Brower Minnoch"