leizongmin / leizongmin/htmlstream-rust
How to get all HTMLTag with name = "a"
Open
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 23
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
Hello,
I want to get all HTMLTag with name = "a" with href & text (anchor) if present..
I'm neewbie with Rust and I do this. But this code doesn't work. Can you help me?
// https://crates.io/crates/htmlstream
extern crate htmlstream;
use htmlstream::*;
// Standard
use std::path::Path;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
fn main() {
let html_file = Path::new("cocon1.html");
// Open the path in read-only mode
let mut file = match File::open(&html_file) {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open {}: {}", html_file.display(), why.description()),
Ok(file) => file,
};
// Read the file contents into a string
let mut html = String::new();
match file.read_to_string(&mut html) {
Err(why) => panic!("couldn't read {}: {}", html_file.display(), why.description()),
Ok(_) => {
let mut iter_tag = htmlstream::tag_iter(&html);
loop {
match iter_tag.next() {
Some(values) => {
let (pos, tag) = values;
if tag.name == "a" && tag.state == HTMLTagState::Opening {
println!("{:?} {:?}", pos, tag);
for (pos, attr) in attr_iter(&tag.attributes) {
if attr.name == "href" {
println!(" => {:?} {:?}", pos, attr.value);
break;
}
}
match iter_tag.next() {
Some(values) => {
let (pos, tag) = values;
if tag.state == HTMLTagState::Text {
println!(" => {:?} {:?}", pos, tag.html);
break;
}
/*if tag.state == HTMLTagState::Closing {
break;
}*/
},
None => break,
}
}
},
None => break,
}
}
},
}
}
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the example around htmlstream::tag_iter and attr_iter, then inspect the HTMLTag state handling shown in the issue. Verify how opening, text, and closing tags are yielded; done means the program reliably reports every tag together with its href and text when present.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100