From: Yuri Khan <yuri.v.khan@gmail.com>
To: Jean Louis <bugs@gnu.support>
Cc: Michael Heerdegen <michael_heerdegen@web.de>,
help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: To fetch URL, extract <title> element?
Date: Thu, 12 Nov 2020 21:49:47 +0700 [thread overview]
Message-ID: <CAP_d_8VupQ5p1s_F=zmhSogs7XZmbLnFr51T6qUjayns0PT57g@mail.gmail.com> (raw)
In-Reply-To: <X6wntvKmc5LtYuiO@protected.rcdrun.com>
On Thu, 12 Nov 2020 at 01:05, Jean Louis <bugs@gnu.support> wrote:
> > > What is the standard built-in way to fetch the http[s] URL?
> >
> > `url-retrieve' sounds appropriate.
>
> I am trying like this:
>
> (defun wrs-fetch-title (url)
> (url-retrieve url #'wrs-get-title (list url)))
>
> (defun wrs-get-title (status url)
> (message-any status))
>
> (wrs-fetch-title "http://localhost")
>
> At least I get status nil, but I do not know how to get the HTML
> text.
Have you read the docstring of ‘url-retrieve’?
CALLBACK is called when the object has been completely retrieved, with
the current buffer containing the object, and any MIME headers associated
with it.[…]
So probably:
(defun wrs-fetch-title (url)
(url-retrieve url #'wrs-get-title (list url)))
(defun wrs-get-title (status url)
(goto-char (point-min))
(search-forward "\n\n") ; skip HTTP headers
(if (search-forward-regexp "<title\\(?:\\s+[^>]*\\)?>\\([^<]*\\)</title>"
nil 'noerror)
(message "URL: %s Title: %s" url (match-string 1))))
(wrs-fetch-title "https://gnu.org/")
⇒ URL: https://gnu.org/ Title: The GNU Operating System and the Free
Software Movement
(For demonstration purposes, I’m overlooking error handling and MIME
type checks. In a real program, you ought to first make sure you got a
successful status, then check that the response you got has a
‘Content-Type’ of either ‘text/html’ or ‘application/xhtml+xml’ (with
possible parameters such as ‘charset’), and only then look for
HTML-specific <title>…</title> tags.)
prev parent reply other threads:[~2020-11-12 14:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-11 9:28 To fetch URL, extract <title> element? Jean Louis
2020-11-11 15:27 ` Michael Heerdegen
2020-11-11 18:04 ` Jean Louis
2020-11-12 12:56 ` Michael Heerdegen
2020-11-12 13:20 ` Jean Louis
2020-11-12 14:49 ` Yuri Khan [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAP_d_8VupQ5p1s_F=zmhSogs7XZmbLnFr51T6qUjayns0PT57g@mail.gmail.com' \
--to=yuri.v.khan@gmail.com \
--cc=bugs@gnu.support \
--cc=help-gnu-emacs@gnu.org \
--cc=michael_heerdegen@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).