Re: http_read_data
Jan Wielemaker <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
On 07/15/2013 10:56 PM, Josef Frydl wrote:
> Please help, I can not figure out a usage of http_read_data in the client side.
> This is what I think I should do:
> open('res.html', read, S)
> http_open('http://www.google.com/search?hl=en&as_q=something', In, [])
> http_read_data(???, Data, [input(In), output(S)])……............
http_read_data/3 is designed to read POST data in servers. What do you
want? Just save the HTML document? The clean code is
safe_url(URL, File) :-
setup_call_cleanup(
http_open(URL, In, []),
setup_call_cleanup(
open(File, write, Out, [type(binary)]),
copy_stream_data(In, Out),
close(Out)),
close(In)).
But, from the discussions, I think you want to analyse the data. Then do
something like this (which picks up all links from the document):
:- use_module(library(http/http_open)).
:- use_module(library(sgml)).
:- use_module(library(xpath)).
links_in_url(URL, Links) :-
setup_call_cleanup(
http_open(URL, In, []),
load_html(In, DOM, []), % Only in recent 6.3.X
close(In)),
findall(Link, xpath(DOM, //a(href(Link)), Links)).
I guess you want something more subtle than just picking all links, which
means some refinement to the xpath query.
Note the use of setup_call_cleanup/3, which is very wise to avoid leaking
resources when talking to unreliable networks. None of the above is tested.
Cheers --- Jan
> But I can not figure out what is the first argument. Doc says parsed header. How do I get it that parsed header.
> Or is there any single example of usage?.
> Thanks Josef Frydl
> -------------- next part --------------
> HTML attachment scrubbed and removed
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>