1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Skyscraper parses HTML documents into structured trees with the help of [indextree].
//! It can then traverse the tree and select nodes using standard XPath expressions either
//! created programatically or parsed from XPath string literals.
//!
//! This crate is split into two main modules: [html] and [xpath].
//!
//! For more information on HTML documents and nodes, including how to get text or attributes from nodes,
//! see the [html] module documentation.
//!
//! For more information on XPath expressions, see the [xpath] module documentation.
//!
//! # Example: parse an HTML document and use an XPath expression
//! ```rust
//! # use std::error::Error;
//! # fn main() -> Result<(), Box<dyn Error>> {
//! use skyscraper::html;
//! use skyscraper::xpath;
//!
//! let text = r##"
//! <html>
//! <body>
//! <div>
//! <div class="no"></div>
//! <div class="duplicate">Bad info</div>
//! </div>
//! <div>
//! <div class="yes"></div>
//! <div class="duplicate">Good info</div>
//! </div>
//! </body>
//! </html>"##;
//!
//! // Parse the HTML text into an XpathItemTree
//! let tree = html::parse(text)?;
//!
//! // Assuming your XPath string is static, it is safe to use `expect` during parsing
//! let xpath = xpath::parse("//div[@class='yes']/parent::div/div[@class='duplicate']")
//! .expect("xpath is invalid");
//!
//! // Apply the XPath expression to our HTML document
//! let items = xpath.apply(&tree)?;
//!
//! assert_eq!(items.len(), 1);
//!
//! // Compare the text of the first and only node returned by the XPath expression
//! let node = items[0].extract_as_node();
//! let text = node.text(&tree).unwrap();
//!
//! assert_eq!(text, "Good info");
//!
//! // Assert that node class attribute is "duplicate" string.
//! let element = node.extract_as_element_node();
//! let attribute = element.get_attribute(&tree, "class").unwrap();
//! assert_eq!(attribute, "duplicate");
//!
//! # Ok(())
//! # }
//! ```
//!
//! # Example: use LazyLock if Xpath expressions are static
//!
//! If your Xpath expressions are static, and you have a function that
//! parses and applies the expression every time the function is called,
//! consider using [`std::sync::LazyLock`] to prevent the expression from being
//! repeatedly parsed.
//!
//! ```rust
//! use std::error::Error;
//! use std::sync::LazyLock;
//! use skyscraper::{html, xpath::{self, Xpath, XpathItemTree}};
//!
//! static SPAN_XPATH: LazyLock<Xpath> = LazyLock::new(|| xpath::parse("//span").unwrap());
//!
//! fn my_func(tree: &XpathItemTree) -> Result<String, Box<dyn Error>> {
//! let result = SPAN_XPATH.apply(tree)?;
//!
//! let items = result;
//! let node = items[0].extract_as_node();
//! Ok(node.text(tree).unwrap())
//! }
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//! let tree1 = html::parse("<div><span>foo</span></div>")?;
//! let text1 = my_func(&tree1)?;
//! assert_eq!(text1, "foo");
//!
//! let tree2 = html::parse("<div><span>bar</span></div>")?;
//! let text2 = my_func(&tree2)?;
//! assert_eq!(text2, "bar");
//!
//! Ok(())
//! }
//! ```