Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple examples #49

Open
UnixJunkie opened this issue Apr 11, 2022 · 3 comments
Open

simple examples #49

UnixJunkie opened this issue Apr 11, 2022 · 3 comments

Comments

@UnixJunkie
Copy link

Hello,

I don't know CSS, but it looks like your library can do what I need.

I would like some simple data extraction example.

If I have such an HTML text:

<td style="background-color:#CCFFCC;" class="_siheader">
TOTO</td>
<td  class="_sibody">
12.34</td>

I would like to extract the TOTO float value (12.34).
How should I go about it?

Thanks a lot,
F.

@UnixJunkie
Copy link
Author

I have to admit that the "open Soup" directive in the README file and documentation completely leaves me blind about
which functions are from the Soup module.
The use of operators like '$', '$$', and '|>' also makes the code very unreadable to me.
I would like to know, in plain ocaml, how to use this library...
I almost feel like all the examples and documentation are about some Haskell code.

@aantron
Copy link
Owner

aantron commented Apr 12, 2022

How should I go about it?

To fully avoid all the operators besides |>, one way is

Soup.parse html_text
|> Soup.select_one "td._sibody"
|> Option.bind Soup.leaf_text
|> Option.map Float.of_string
|> Option.get

I have to admit that the "open Soup" directive in the README file and documentation completely leaves me blind about
which functions are from the Soup module.

All the ones that aren't from the standard library are from Soup. The library documentation assumes the reader recognizes identifiers from the OCaml standard library, and looks in the library documentation for the rest. In my own programming I indeed don't do open Soup, but I do often do open Soup.Infix to get just $, $$, $?, or else write out their replacements (Soup.select family).

The use of operators like '$', '$$', and '|>' also makes the code very unreadable to me.

|> in particular is a standard operator that is very heavily used in OCaml programming. At least this operator is plain OCaml.

The $ and $$ operators are very simply defined in the library documentation.

I would like to know, in plain ocaml, how to use this library...

See the standard library and Lambda Soup documentation (all written in plain OCaml).

I almost feel like all the examples and documentation are about some Haskell code.

Could you adjust this to feel like it's jQuery instead? This is just ordinary "pipelining" or "chaining" as used in JS and throughout the programming world.

@UnixJunkie
Copy link
Author

Thanks for the example. I'll try to adapt some code I have to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants