Tag

Struct Tag 

Source
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

  • Copy trait enables cheap passing without ownership concerns
  • Lifetime 'a tied to Document prevents dangling references
  • NodeId enables 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>

Source

pub fn node_id(&self) -> NodeId

Returns the node ID.

Source

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"));
}
Source

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);
}
Source

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"));
}
Source

pub fn attrs(&self) -> Option<&HashMap<String, String>>

Returns all attributes on this element.

Returns None if this is not an element node.

Source

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"));
}
Source

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"]);
}
Source

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!");
}
Source

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>");
}
Source

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>");
}
Source

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"));
}
Source

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);
}
Source

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"));
    }
}
Source

pub fn prev_sibling(&self) -> Option<Tag<'a>>

Returns the previous sibling element.

Skips text and comment nodes.

Source

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).

Source

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());
}
Source

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);
}
Source

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.

Trait Implementations§

Source§

impl<'a> Clone for Tag<'a>

Source§

fn clone(&self) -> Tag<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Tag<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Tag<'_>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Copy for Tag<'a>

Source§

impl Eq for Tag<'_>

Auto Trait Implementations§

§

impl<'a> Freeze for Tag<'a>

§

impl<'a> RefUnwindSafe for Tag<'a>

§

impl<'a> Send for Tag<'a>

§

impl<'a> Sync for Tag<'a>

§

impl<'a> Unpin for Tag<'a>

§

impl<'a> UnwindSafe for Tag<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.