Recipe: How to get a page from the Internet
Last updated at 9:31 pm UTC on 10 January 2022
Problem
You want to retrieve a web page on the Internet for which you have the URL.
Solution
"Output a Web page on the Transcript window"
| mimeDoc |
mimeDoc := 'http://www.squeak.org/' asUrl retrieveContents.
Transcript show: mimeDoc content.
Discussion
The method #asUrl takes a string and converts it to an URL. URLs are represented as concrete subinstances of Url (one for each protocol).
'http://www.squeak.org/' asUrl class
HttpUrl
The method retrieveContents asks a Url to retrieve its contents and return a MIMEDocument. A MIMEDocument contains the actual data along with a MIME content-type; content will return the data.
Showing the page in its own TwoWayScrollPane.
| mimeDoc |
urlString := 'http://www.squeak.org/'.
mimeDoc := urlString asUrl retrieveContents.
StringHolder new textContents: mimeDoc content withSqueakLineEndings; openLabel: urlString.
Checked with Squeak version 3.10.2: OK.
Or popping up an ObjectExplorer on the parsed HTML document
| mimeDoc |
urlString := 'http://www.squeak.org/'.
mimeDoc := urlString asUrl retrieveContents.
(HtmlParser parse: mimeDoc content) explore
The same in one line
(HtmlParser parse: 'http://www.squeak.org/' asUrl retrieveContents content) explore
For more details, browse classes Url and MIMEDocument in the system.
Squeak version 3.10.2 does not contain the class HtmlParser anymore: you have to load the package Network-HTML through the package universe.
Then it runs fine with 3.10.2.
October 2020: Checked with Squeak version 5.3: OK.
Also see: