| 1 | # select.rs  [](https://docs.rs/select/) |
| 2 | |
| 3 | > A library to extract useful data from HTML documents, suitable for web scraping. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## Examples |
| 8 | |
| 9 | ### from [`examples/stackoverflow.rs`](examples/stackoverflow.rs) |
| 10 | |
| 11 | ```rust |
| 12 | use select::document::Document; |
| 13 | use select::predicate::{Attr, Class, Name, Predicate}; |
| 14 | |
| 15 | pub fn main() { |
| 16 | // stackoverflow.html was fetched from |
| 17 | // http://stackoverflow.com/questions/tagged/rust?sort=votes&pageSize=50 on |
| 18 | // Aug 10, 2015. |
| 19 | let document = Document::from(include_str!("stackoverflow.html")); |
| 20 | |
| 21 | println!("# Menu"); |
| 22 | for node in document.find(Attr("id", "hmenus").descendant(Name("a"))) { |
| 23 | println!("{} ({:?})", node.text(), node.attr("href").unwrap()); |
| 24 | } |
| 25 | println!(); |
| 26 | |
| 27 | println!("# Top 5 Questions"); |
| 28 | for node in document.find(Class("question-summary")).take(5) { |
| 29 | let question = node.find(Class("question-hyperlink")).next().unwrap(); |
| 30 | let votes = node.find(Class("vote-count-post")).next().unwrap().text(); |
| 31 | let answers = node |
| 32 | .find(Class("status").descendant(Name("strong"))) |
| 33 | .next() |
| 34 | .unwrap() |
| 35 | .text(); |
| 36 | let tags = node |
| 37 | .find(Class("post-tag")) |
| 38 | .map(|tag| tag.text()) |
| 39 | .collect::<Vec<_>>(); |
| 40 | let asked_on = node.find(Class("relativetime")).next().unwrap().text(); |
| 41 | let asker = node |
| 42 | .find(Class("user-details").descendant(Name("a"))) |
| 43 | .next() |
| 44 | .unwrap() |
| 45 | .text(); |
| 46 | println!(" Question: {}", question.text()); |
| 47 | println!(" Answers: {}", answers); |
| 48 | println!(" Votes: {}", votes); |
| 49 | println!(" Tagged: {}", tags.join(", ")); |
| 50 | println!(" Asked on: {}", asked_on); |
| 51 | println!(" Asker: {}", asker); |
| 52 | println!( |
| 53 | "Permalink: http://stackoverflow.com{}", |
| 54 | question.attr("href").unwrap() |
| 55 | ); |
| 56 | println!(); |
| 57 | } |
| 58 | |
| 59 | println!("# Top 10 Related Tags"); |
| 60 | for node in document |
| 61 | .find(Attr("id", "h-related-tags")) |
| 62 | .next() |
| 63 | .unwrap() |
| 64 | .parent() |
| 65 | .unwrap() |
| 66 | .find(Name("div")) |
| 67 | .take(10) |
| 68 | { |
| 69 | let tag = node.find(Name("a")).next().unwrap().text(); |
| 70 | let count = node |
| 71 | .find(Class("item-multiplier-count")) |
| 72 | .next() |
| 73 | .unwrap() |
| 74 | .text(); |
| 75 | println!("{} ({})", tag, count); |
| 76 | } |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | prints |
| 81 | |
| 82 | ``` |
| 83 | # Menu |
| 84 | Questions ("/questions") |
| 85 | Tags ("/tags") |
| 86 | Users ("/users") |
| 87 | Badges ("/help/badges") |
| 88 | Unanswered ("/unanswered") |
| 89 | Ask Question ("/questions/ask") |
| 90 | |
| 91 | # Top 5 Questions |
| 92 | Question: Applications and libraries written in Rust [closed] |
| 93 | Answers: 8 |
| 94 | Votes: 67 |
| 95 | Tagged: rust |
| 96 | Asked on: Feb 19 '12 at 14:39 |
| 97 | Asker: Atom |
| 98 | Permalink: http://stackoverflow.com/questions/9350125/applications-and-libraries-written-in-rust |
| 99 | |
| 100 | Question: How to debug Rust programs? [closed] |
| 101 | Answers: 6 |
| 102 | Votes: 52 |
| 103 | Tagged: rust |
| 104 | Asked on: Apr 8 '13 at 5:30 |
| 105 | Asker: macropas |
| 106 | Permalink: http://stackoverflow.com/questions/15871885/how-to-debug-rust-programs |
| 107 | |
| 108 | Question: How to access command line parameters? |
| 109 | Answers: 9 |
| 110 | Votes: 51 |
| 111 | Tagged: rust |
| 112 | Asked on: Mar 25 '13 at 15:59 |
| 113 | Asker: shutefan |
| 114 | Permalink: http://stackoverflow.com/questions/15619320/how-to-access-command-line-parameters |
| 115 | |
| 116 | Question: Why are explicit lifetimes needed in Rust? |
| 117 | Answers: 6 |
| 118 | Votes: 48 |
| 119 | Tagged: pointers, rust, static-analysis, lifetime |
| 120 | Asked on: Jul 24 at 11:15 |
| 121 | Asker: jco |
| 122 | Permalink: http://stackoverflow.com/questions/31609137/why-are-explicit-lifetimes-needed-in-rust |
| 123 | |
| 124 | Question: What is the difference between traits in Rust and typeclasses in Haskell? |
| 125 | Answers: 1 |
| 126 | Votes: 46 |
| 127 | Tagged: haskell, rust |
| 128 | Asked on: Jan 24 at 7:50 |
| 129 | Asker: LogicChains |
| 130 | Permalink: http://stackoverflow.com/questions/28123453/what-is-the-difference-between-traits-in-rust-and-typeclasses-in-haskell |
| 131 | |
| 132 | # Top 10 Related Tags |
| 133 | lifetime (165) |
| 134 | traits (83) |
| 135 | rust-cargo (79) |
| 136 | string (76) |
| 137 | ffi (62) |
| 138 | iterator (58) |
| 139 | multithreading (50) |
| 140 | generics (50) |
| 141 | arrays (49) |
| 142 | borrow-checker (47) |
| 143 | ``` |
| 144 | |
| 145 | ## License |
| 146 | |
| 147 | MIT |