我正在尝试获取标题为“Name”的列下的第一行,例如 https://en.wikipedia.org/wiki/List_of_the_heaviest_people我想返回名称“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")
我对 xml 不是很好,不知道如何在某个列标题下获取 td。
最佳答案
html5-parser:parse-html5
返回的文档中的元素形式为:
("name" (attribute-alist) &rest children)
您可以使用标准列表操作函数访问部件,但是 xmls
还提供功能node-name
, node-attrs
和 node-children
访问这三个部分。使用它们会更清楚一些。 编辑:还有函数xmlrep-attrib-value
, 获取属性值和 xmlrep-tagmatch
以匹配标签名称。子项可以是纯字符串,也可以是相同格式的元素。
例如,带有 2x2 表格的 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"
关于lisp - 使用 html5-parser 和 xmls Common Lisp 浏览网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35065686/