pub struct Tag<'a> { /* private fields */ }Expand description
A reference to an element in the document.
Tag provides navigation and content extraction methods. It borrows from
the underlying Document, ensuring the tag remains valid while in use.
§Design
Copytrait enables cheap passing without ownership concerns- Lifetime
'atied to Document prevents dangling references NodeIdenables O(1) node access via arena
§Examples
§Accessing Attributes
use scrape_core::Soup;
let soup = Soup::parse("<a href=\"https://example.com\" class=\"link\">Link</a>");
if let Ok(Some(link)) = soup.find("a") {
assert_eq!(link.get("href"), Some("https://example.com"));
assert!(link.has_class("link"));
}§Tree Navigation
use scrape_core::Soup;
let soup = Soup::parse("<div><span>Child</span></div>");
if let Ok(Some(span)) = soup.find("span") {
if let Some(parent) = span.parent() {
assert_eq!(parent.name(), Some("div"));
}
}Implementations§
Source§impl<'a> Tag<'a>
impl<'a> Tag<'a>
Sourcepub fn name(&self) -> Option<&str>
pub fn name(&self) -> Option<&str>
Returns the tag name (e.g., “div”, “span”, “a”).
Returns None if this is not an element node.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div></div>");
if let Ok(Some(div)) = soup.find("div") {
assert_eq!(div.name(), Some("div"));
}Sourcepub fn get(&self, attr: &str) -> Option<&str>
pub fn get(&self, attr: &str) -> Option<&str>
Returns the value of an attribute, if present.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<a href=\"/page\">Link</a>");
if let Ok(Some(link)) = soup.find("a") {
assert_eq!(link.get("href"), Some("/page"));
assert_eq!(link.get("class"), None);
}Sourcepub fn has_attr(&self, attr: &str) -> bool
pub fn has_attr(&self, attr: &str) -> bool
Checks if this element has the specified attribute.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<input disabled type=\"text\">");
if let Ok(Some(input)) = soup.find("input") {
assert!(input.has_attr("disabled"));
assert!(input.has_attr("type"));
assert!(!input.has_attr("value"));
}Sourcepub fn attrs(&self) -> Option<&HashMap<String, String>>
pub fn attrs(&self) -> Option<&HashMap<String, String>>
Returns all attributes on this element.
Returns None if this is not an element node.
Sourcepub fn has_class(&self, class: &str) -> bool
pub fn has_class(&self, class: &str) -> bool
Checks if this element has the specified class.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div class=\"foo bar\"></div>");
if let Ok(Some(div)) = soup.find("div") {
assert!(div.has_class("foo"));
assert!(div.has_class("bar"));
assert!(!div.has_class("baz"));
}Sourcepub fn classes(&self) -> impl Iterator<Item = &str>
pub fn classes(&self) -> impl Iterator<Item = &str>
Returns all classes on this element.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div class=\"foo bar baz\"></div>");
if let Ok(Some(div)) = soup.find("div") {
let classes: Vec<_> = div.classes().collect();
assert_eq!(classes, vec!["foo", "bar", "baz"]);
}Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Returns the text content of this element and its descendants.
HTML tags are stripped and only text nodes are included. Text from multiple nodes is concatenated with no separator.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div>Hello <b>World</b>!</div>");
if let Ok(Some(div)) = soup.find("div") {
assert_eq!(div.text(), "Hello World!");
}Sourcepub fn inner_html(&self) -> String
pub fn inner_html(&self) -> String
Returns the inner HTML of this element.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div><span>Hello</span></div>");
if let Ok(Some(div)) = soup.find("div") {
assert_eq!(div.inner_html(), "<span>Hello</span>");
}Sourcepub fn outer_html(&self) -> String
pub fn outer_html(&self) -> String
Returns the outer HTML of this element (including the tag itself).
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div><span>Hello</span></div>");
if let Ok(Some(span)) = soup.find("span") {
assert_eq!(span.outer_html(), "<span>Hello</span>");
}Sourcepub fn parent(&self) -> Option<Tag<'a>>
pub fn parent(&self) -> Option<Tag<'a>>
Returns the parent element, if any.
Returns None for the root element or if the parent is not an element.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div><span>text</span></div>");
if let Ok(Some(span)) = soup.find("span") {
let parent = span.parent().unwrap();
assert_eq!(parent.name(), Some("div"));
}Sourcepub fn children(&self) -> impl Iterator<Item = Tag<'a>>
pub fn children(&self) -> impl Iterator<Item = Tag<'a>>
Returns an iterator over direct child elements.
Only element nodes are included (text and comments are skipped).
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<ul><li>A</li><li>B</li><li>C</li></ul>");
if let Ok(Some(ul)) = soup.find("ul") {
let children: Vec<_> = ul.children().collect();
assert_eq!(children.len(), 3);
}Sourcepub fn next_sibling(&self) -> Option<Tag<'a>>
pub fn next_sibling(&self) -> Option<Tag<'a>>
Returns the next sibling element.
Skips text and comment nodes.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<ul><li id=\"a\">A</li><li id=\"b\">B</li></ul>");
if let Ok(Some(first)) = soup.find("li") {
if let Some(next) = first.next_sibling() {
assert_eq!(next.get("id"), Some("b"));
}
}Sourcepub fn prev_sibling(&self) -> Option<Tag<'a>>
pub fn prev_sibling(&self) -> Option<Tag<'a>>
Returns the previous sibling element.
Skips text and comment nodes.
Sourcepub fn descendants(&self) -> impl Iterator<Item = Tag<'a>>
pub fn descendants(&self) -> impl Iterator<Item = Tag<'a>>
Returns an iterator over all descendant elements.
Only element nodes are included (text and comments are skipped).
Sourcepub fn find(&self, selector: &str) -> QueryResult<Option<Tag<'a>>>
pub fn find(&self, selector: &str) -> QueryResult<Option<Tag<'a>>>
Finds the first descendant matching the selector.
§Errors
Returns [QueryError::InvalidSelector] if the selector syntax is invalid.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<div><ul><li class=\"item\">text</li></ul></div>");
if let Ok(Some(div)) = soup.find("div") {
let item = div.find(".item").unwrap();
assert!(item.is_some());
}Sourcepub fn find_all(&self, selector: &str) -> QueryResult<Vec<Tag<'a>>>
pub fn find_all(&self, selector: &str) -> QueryResult<Vec<Tag<'a>>>
Finds all descendants matching the selector.
§Errors
Returns [QueryError::InvalidSelector] if the selector syntax is invalid.
§Examples
use scrape_core::Soup;
let soup = Soup::parse("<ul><li>A</li><li>B</li><li>C</li></ul>");
if let Ok(Some(ul)) = soup.find("ul") {
let items = ul.find_all("li").unwrap();
assert_eq!(items.len(), 3);
}Sourcepub fn select(&self, selector: &str) -> QueryResult<Vec<Tag<'a>>>
pub fn select(&self, selector: &str) -> QueryResult<Vec<Tag<'a>>>
Selects descendants using a CSS selector.
Alias for Tag::find_all.
§Errors
Returns [QueryError::InvalidSelector] if the selector syntax is invalid.