-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from holmofy/extract-macro
Extract macro
- Loading branch information
Showing
16 changed files
with
835 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,80 @@ | ||
use anyhow::Result; | ||
use reqwest_scraper::ScraperResponse; | ||
use reqwest_scraper::{FromCssSelector, ScraperResponse}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
request().await.expect("request error"); | ||
} | ||
|
||
#[derive(Debug, FromCssSelector)] | ||
#[selector(path = "#user-repositories-list > ul > li")] | ||
struct Repo { | ||
#[selector(path = "a[itemprop~='name']", default = "<unname>", text)] | ||
name: String, | ||
|
||
#[selector(path = "span[itemprop~='programmingLanguage']", text)] | ||
program_lang: Option<String>, | ||
|
||
#[selector(path = "div.topics-row-container>a", text)] | ||
topics: Vec<String>, | ||
} | ||
|
||
async fn request() -> Result<()> { | ||
let html = reqwest::get("https://github.com/holmofy") | ||
.await? | ||
.css_selector() | ||
.await?; | ||
|
||
// 1. Simple extract | ||
assert_eq!( | ||
html.select(".p-name")?.iter().nth(0).unwrap().text().trim(), | ||
html.select(".p-name")? | ||
.first() | ||
.map(|e| e.text()) | ||
.unwrap_or("xxx".into()), | ||
"holmofy" | ||
); | ||
|
||
let select_result = html.select(".vcard-details > li.vcard-detail")?; | ||
let html = reqwest::get("https://github.com/holmofy?tab=repositories") | ||
.await? | ||
.css_selector() | ||
.await?; | ||
|
||
// 2. Select List Element | ||
println!("\n2. Select List Element"); | ||
let select_result = html.select("#user-repositories-list > ul > li")?; | ||
|
||
for item in select_result.iter() { | ||
let name = item | ||
.select("a[itemprop~='name']")? | ||
.first() | ||
.map(|e| e.text()) | ||
.unwrap_or("<unname>".into()); | ||
|
||
let program_lang = item | ||
.select("span[itemprop~='programmingLanguage']")? | ||
.first() | ||
.map(|e| e.text()); | ||
|
||
let topics = item | ||
.select("div.topics-row-container>a")? | ||
.iter() | ||
.map(|e| e.text()) | ||
.collect::<Vec<_>>(); | ||
|
||
for detail_item in select_result.iter() { | ||
println!( | ||
"{}", | ||
detail_item.attr("aria-label").unwrap_or_else(|| "".into()) | ||
) | ||
let item = Repo { | ||
name, | ||
program_lang, | ||
topics, | ||
}; | ||
|
||
println!("{:?}", item); | ||
} | ||
|
||
// 3. Extract By Derived Macros | ||
println!("\n3. Extract By Derived Macros"); | ||
|
||
let items = Repo::from_html(html)?; | ||
items.iter().for_each(|item| println!("{:?}", item)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "reqwest-scraper-macros" | ||
version = "0.3.0" | ||
edition = "2021" | ||
description = "Web scraping integration with reqwest" | ||
license-file = "LICENSE" | ||
repository = "https://github.com/holmofy/reqwest-scraper" | ||
|
||
[lib] | ||
name = "reqwest_scraper_macros" | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "2.0" | ||
darling = "0.20.10" | ||
scraper = { version = "0.19", default-features = false } | ||
|
||
[dev-dependencies] | ||
reqwest-scraper = { version = "0.3.0", path = "../" } |
Oops, something went wrong.