Skip to content

Instantly share code, notes, and snippets.

@kxzk
Last active January 5, 2018 23:03
Show Gist options
  • Save kxzk/8073d726468043de79567bc4f4994fbe to your computer and use it in GitHub Desktop.
Save kxzk/8073d726468043de79567bc4f4994fbe to your computer and use it in GitHub Desktop.
Grabbing the Rank, Headline and URL for Hacker News
extern crate reqwest;
extern crate select;
use select::document::Document;
use select::predicate::{Class, Name, Predicate};
fn main() {
hacker_news("https://news.ycombinator.com");
}
fn hacker_news(url: &str) {
let resp = reqwest::get(url).unwrap();
assert!(resp.status().is_success());
let document = Document::from_read(resp).unwrap();
// finding all instances of our class of interest
for node in document.find(Class("athing")) {
// grabbing the story rank
let rank = node.find(Class("rank")).next().unwrap();
// finding class, then selecting article title
let story = node.find(Class("title").descendant(Name("a")))
.next()
.unwrap()
.text();
// printing out | rank | story headline
println!("\n | {} | {}\n", rank.text(), story);
// same as above
let url = node.find(Class("title").descendant(Name("a"))).next().unwrap();
// however, we don't grab text
// instead find the "href" attribute, which gives us the url
println!("{:?}\n", url.attr("href").unwrap());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment