elisp - 从 Emacs 缓冲区中提取 URL?

标签 elisp

如何编写 Emacs Lisp 函数来查找 HTML 文件中的所有 href 并提取所有链接?

输入:

<html>
 <a href="http://www.stackoverflow.com" _target="_blank">StackOverFlow</a>
 <h1>Emacs Lisp</h1>
 <a href="http://news.ycombinator.com" _target="_blank">Hacker News</a>
</html>

输出:

http://www.stackoverflow.com|StackOverFlow
http://news.ycombinator.com|Hacker News

在搜索过程中,我曾多次看到重新搜索转发功能。根据我目前所读到的内容,这是我认为我需要做的事情。

(defun extra-urls (file)
 ...
 (setq buffer (...
 (while
        (re-search-forward "http://" nil t)
        (when (match-string 0)
...
))

最佳答案

我采用了 Heinzi 的解决方案,并提出了我需要的最终解决方案。我现在可以获取文件列表,提取所有 URL 和标题,并将结果放在一个输出缓冲区中。

(defun extract-urls (fname)
 "Extract HTML href url's,titles to buffer 'new-urls.csv' in | separated format."
  (setq in-buf (set-buffer (find-file fname))); Save for clean up
  (beginning-of-buffer); Need to do this in case the buffer is already open
  (setq u1 '())
  (while
      (re-search-forward "^.*<a href=\"\\([^\"]+\\)\"[^>]+>\\([^<]+\\)</a>" nil t)

      (when (match-string 0)            ; Got a match
        (setq url (match-string 1) )    ; URL
        (setq title (match-string 2) )  ; Title
        (setq u1 (cons (concat url "|" title "\n") u1)) ; Build the list of URLs
       )
      )
  (kill-buffer in-buf)          ; Don't leave a mess of buffers
  (progn
    (with-current-buffer (get-buffer-create "new-urls.csv"); Send results to new buffer
      (mapcar 'insert u1))
    (switch-to-buffer "new-urls.csv"); Finally, show the new buffer
    )
  )

;; Create a list of files to process
;;
(mapcar 'extract-urls '(
                       "/tmp/foo.html"
                       "/tmp/bar.html"
               ))

关于elisp - 从 Emacs 缓冲区中提取 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1642184/

相关文章:

emacs - 为什么我的框架标题格式不起作用?

javascript - 在 javascript 模式 emacs 中用 f(x) 替换 function(x)

emacs - 用区域上调用函数的结果替换区域

emacs - 如何在启动时启用 `fill-column-indicator`

emacs - Emacs主题定义中,为什么会有(t ( :background "black"))

emacs - 如何在 Emacs Lisp 的文字中使用变量名?

regex - Emacs Lisp 和非确定性正则表达式

emacs - Elisp:如何从具有字符串键的关联列表中删除元素

Emacs align-regexp on = 但不是 ==

emacs - 字符串到数字和插入给出控制字符(例如 ^C、^D)而不是数字(例如 3、4)