Skip to content

Commit 65ae992

Browse files
committed
Add simplified working version
1 parent 6931d02 commit 65ae992

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

hack-scraper/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "hack-scraper"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
reqwest = { version = "0.11", features = ["blocking"] }
10+
scraper = "0.13.0"
11+
prettytable-rs = "0.9.0"

hack-scraper/src/main.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
extern crate reqwest;
2+
#[macro_use] extern crate prettytable;
3+
use prettytable::Table;
4+
use scraper::{Html, Selector};
5+
6+
fn get_hacker_news_data() -> Result<String, Box<dyn std::error::Error>> {
7+
let hn_txt = reqwest::blocking::get("https://news.ycombinator.com/")?
8+
.text()?;
9+
10+
Ok(hn_txt)
11+
}
12+
13+
fn main() {
14+
let hn_txt = get_hacker_news_data().unwrap();
15+
16+
let document = Html::parse_document(&hn_txt);
17+
18+
let stories = Selector::parse("td:nth-child(3) > span > a").unwrap();
19+
20+
let mut table = Table::new();
21+
22+
for story in document.select(&stories) {
23+
let story_link = story.value().attr("href").unwrap();
24+
let story_txt = story.text().collect::<Vec<_>>();
25+
26+
if story_txt[0] == "login" {
27+
continue;
28+
}
29+
30+
table.add_row(row![FdBybl->story_txt[0]]);
31+
table.add_row(row![Fy->story_link]);
32+
}
33+
34+
table.printstd();
35+
}

0 commit comments

Comments
 (0)