Elisp是否有一个函数可以接受URL和目标路径并从互联网下载该URL的内容?
我已经发现了url-retrieve
和url-retrieve-synchronously
,但是url-retrieve
需要回调函数,而url-retrieve-synchronously
将所有内容放入缓冲区。是否有更简单的方法?
试试使用url-copy-file
函数。它的描述如下:
url-copy-file
是`url-handlers.el'中一个自动加载的Lisp函数。(url-copy-file url newname &optional ok-if-already-exists keep-time)
将URL复制到新文件名newname中。两个参数都必须是字符串类型。 如果文件newname已经存在,则会发出“file-already-exists”错误, 除非提供了第三个参数ok-if-already-exists并且其值为非nil。 如果第三个参数是数字,表示请求在newname已经存在时进行确认。 这就是通过M-x进行交互式使用时发生的情况。 第四个参数keep-time的值为非nil,则会将新文件的最后修改时间设置为与旧文件相同。 (这仅适用于某些系统)。前缀参数使keep-time的值为非nil。
require 'url'
才能使用url-copy-file
。 - Natewget
一样使用它(即不作为程序或函数的一部分),你可以输入ESC:
(这将提示输入一个Elisp表达式进行评估),然后输入以下内容:(url-copy-file "http://example.com/" "filename.txt")
- Ashton Wiersdorfurl-copy-file
是最佳选择,但对于更加冒险的 Emacs 程序员,我建议尝试以下方法:(require 'url)
(defun download-file (&optional url download-dir download-name)
(interactive)
(let ((url (or url
(read-string "Enter download URL: "))))
(let ((download-buffer (url-retrieve-synchronously url)))
(save-excursion
(set-buffer download-buffer)
;; we may have to trim the http response
(goto-char (point-min))
(re-search-forward "^$" nil 'move)
(forward-char)
(delete-region (point-min) (point))
(write-file (concat (or download-dir
"~/downloads/")
(or download-name
(car (last (split-string url "/" t))))))))))
(w3m-download "http://www.gnu.org/index.html")
url-copy-file
可以在 w3m 没有移植到的平台上运行。 - JasonFruithttp://steloflute.tistory.com/entry/Emacs-Lisp-urlretrieve
; synchronously
(defun get-url (url)
(with-current-buffer (url-retrieve-synchronously url) (buffer-string)))
(print (get-url "http://www.gnu.org"))
; asynchronously
(defun print-url (url)
(url-retrieve url (lambda (a) (print a))))
(print-url "http://www.gnu.org")
获取URL | http://www.gnu.org/software/emacs/manual/html_node/url/Retrieving-URLs.html
当前缓冲区 | http://www.gnu.org/software/emacs/manual/html_node/elisp/Current-Buffer.html
browse-url-emacs
是一个方便的工具。 - phils