Elisp: Command to Extract URL

By Xah Lee. Date: . Last updated: .

This page shows a command to extract all URLs in a HTML file.

For example, if you have this text:

<a href="../cats.html">cats</a>, <a href="http://en.wikipedia.org/wiki/Idiom">Idiom</a>, <a class="b" href="computing.html"></a>

After calling the command, the following is copied to kill-ring:


If there's no text selection, current paragraph is used.

If universal-argument is called first, relative URLs are converted to full path.


There are many ways to code this. Here's one:

(defun xah-html-extract-url (@begin @end &optional @full-path-p)
  "Extract URLs in current block or region to `kill-ring'.

If `universal-argument' is called first, convert relative URL to full path.

This command extracts all text of the forms
 <‹letter› … href=\"…\" …>
 <‹letter› … src=\"…\" …>
that is on a a single line, by regex. The quote may be single quote.

When called in lisp code, *begin *end are region begin/end positions.
Returns a list.

URL `http://ergoemacs.org/emacs/elisp_extract_url_command.html'
Version 2016-07-28"
   (let ($p1 $p2)
     ;; set region boundary -p1 -p2
     (if (use-region-p)
         (progn (setq $p1 (region-beginning))
                (setq $p2 (region-end)))
         (if (re-search-backward "\n[ \t]*\n" nil "NOERROR")
             (progn (re-search-forward "\n[ \t]*\n")
                    (setq $p1 (point)))
           (setq $p1 (point)))
         (if (re-search-forward "\n[ \t]*\n" nil "NOERROR")
             (progn (re-search-backward "\n[ \t]*\n")
                    (setq $p2 (point)))
           (setq $p2 (point)))))
     (list $p1 $p2 current-prefix-arg)))

  (let (($regionText (buffer-substring-no-properties @begin @end))
        ($urlList (list)))
      (insert $regionText)

      (goto-char 1)
      (while (re-search-forward "<" nil t)
        (replace-match "\n<" "FIXEDCASE" "LITERAL"))

      (goto-char 1)
      (while (re-search-forward
              "<[A-Za-z]+.+?\\(href\\|src\\)[[:blank:]]*?=[[:blank:]]*?\\([\"']\\)\\([^\"']+?\\)\\2" nil t)
        (push (match-string 3) $urlList)))
    (setq $urlList (reverse $urlList))

    (when @full-path-p
      (setq $urlList
             (lambda ($x)
               (if (string-match "^http:\\|^https:" $x )
                   (progn $x)
                   (expand-file-name $x (file-name-directory (buffer-file-name))))))

    (when (called-interactively-p 'any)
      (let (($printedResult (mapconcat 'identity $urlList "\n")))
        (kill-new $printedResult)
        (message "%s" $printedResult)))
    $urlList ))

for latest updates of this code, see Emacs: Xah HTML Mode.

Liket it? Put $1 at patreon. Or Buy Xah Emacs Tutorial. Thanks.