Documentation

### This is machine translation

Mouseover text to see original. Click the button below to return to the English version of the page.

# findElement

Find elements in HTML tree

## Syntax

``subtrees = findElement(tree,selector)``

## Description

example

````subtrees = findElement(tree,selector)` returns the elements in `tree` matching the CSS selector.```

## Examples

collapse all

Read HTML code from the URL `https://www.mathworks.com/help/textanalytics` using the `webread` function.

```url = "https://www.mathworks.com/help/textanalytics"; code = webread(url);```

Parse the HTML code using `htmlTree`.

`tree = htmlTree(code);`

Find all the hyperlinks in the HTML tree using `findElement`. The hyperlinks are nodes with element name `"A"`.

```selector = "A"; subtrees = findElement(tree,selector);```

View the first few subtrees.

`subtrees(1:10)`
```ans = 10×1 htmlTree: (1,1) <A class="svg_link navbar-brand" href="https://www.mathworks.com?s_ti… (2,1) <A class="mwa-nav_login" href="https://www.mathworks.com/login?uri=ht… (3,1) <A href="https://www.mathworks.com/products.html?s_tid=gn_ps">Product… (4,1) <A href="https://www.mathworks.com/solutions.html?s_tid=gn_sol">Solut… (5,1) <A href="https://www.mathworks.com/academia.html?s_tid=gn_acad">Acade… (6,1) <A href="https://www.mathworks.com/support.html?s_tid=gn_supp">Suppor… (7,1) <A href="https://www.mathworks.com/matlabcentral/?s_tid=gn_mlc">Commu… (8,1) <A href="https://www.mathworks.com/company/events.html?s_tid=gn_ev">E… (9,1) <A href="https://www.mathworks.com/company/aboutus/contact_us.html?s_… (10,1) <A href="https://www.mathworks.com/store?s_cid=store_top_nav&amp;s_ti… ```

Extract the text from the subtrees using `extractHTMLText`. The result contains the link text from each link on the page.

```str = extractHTMLText(subtrees); str(1:10)```
```ans = 10×1 string array "" "Sign In" "Products" "Solutions" "Academia" "Support" "Community" "Events" "Contact Us" "How to Buy" ```

## Input Arguments

collapse all

HTML tree, specified as a scalar `htmlTree` object.

CSS selector, specified as a string scalar or a character vector. For more information, see CSS Selectors.

## Output Arguments

collapse all

Matching HTML subtrees, returned as an `htmlTree` array.

collapse all

### HTML Elements

A typical HTML element contains the following components:

• Element name – Name of the HTML tag. The element name corresponds to the `Name` property of the HTML tree.

• Attributes – Additional information about the tag. HTML attributes have the form `name="value"`, where `name` and `value` denote the attribute name and value respectively. The attributes appear inside the opening HTML tag. To get the attribute values from an HTML tree, use `getAttribute`.

• Content – Element content. The content appears between opening and closing HTML tags. The content can be text data or nested HTML elements. To extract the text from an `htmlTree` object, use `extractHTMLText`. To get the nested HTML elements of an `htmlTree` object, use the `Children` property.

For example, the HTML element ```<a href="https://www.mathworks.com">Home</a>``` comprises the following components:

ComponentValueDescription
Element name`a`Element is a hyperlink
AttributeAttribute name`href`Hyperlink reference
Attribute value`"https://www.mathworks.com"`Hyperlink reference value
Content`Home`Text to display

### CSS Selectors

CSS selectors specify patterns to match elements in a tree.

This table shows some examples showing how to extract different HTML elements from an HTML tree:

Find all paragraph (`<p>`) elements.`"p"``findElement(tree,"p")`
Find all paragraph (`<p>`) and list item (`<li>`) elements.`"p,li"``findElement(tree,"p,li")`
Find all paragraph (`<p>`) elements that are inside table (`<table>`) elements.`"table p"``findElement(tree,"table p")`
Find all hyperlink (`<a>`) elements with hyperlink reference attribute (`href`) values ending with `".pdf"`.`"a[href\$="".pdf""]"``findElement(tree,"a[href\$="".pdf""]")`
Find all paragraph (`<p>`) elements that are the first child of their parent.`"p:first-child"``findElement(tr,"p:first-child")`
Find all paragraph (`<p>`) elements that are the first paragraph element of their parent.`"p:first-of-type"``findElement(tr,"p:first-of-type")`
Find all emphasis (`<em>`) elements where the parent is a paragraph (`<p>`) element.`"p > em"``findElement(tr,"p > em")`
Find all paragraph (`<p>`) elements appearing immediately after a heading 1 (`<h1>`) element`"h1 + p"``findElement(tr,"h1 + p")`
Find all empty elements.`":empty"``findElement(tr,":empty")`
Find all nonempty label (`<label>`) elements.`"label:not(:empty)"``findElement(tr,"label:not(:empty)")`

The `findElement` function supports all of CSS level 3, except for the selectors `":lang"`, `":checked"`, `":link"`, `":active"`, `":hover"`, `":focus"`, `":target"`, `":enabled"`, and `":disabled"`.
{"ft_lang_label":"__label__en","ft_lang_prob":0.51261646,"math_prob":0.52908355,"size":1535,"snap":"2019-26-2019-30","text_gpt3_token_len":412,"char_repetition_ratio":0.22403657,"word_repetition_ratio":0.09389672,"special_character_ratio":0.29837134,"punctuation_ratio":0.1993007,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9593728,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-19T13:22:23Z\",\"WARC-Record-ID\":\"<urn:uuid:77abf585-6135-4d74-b234-42cad04eb8d6>\",\"Content-Length\":\"91888\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:247f576a-68e5-401d-a3fc-16b567869542>\",\"WARC-Concurrent-To\":\"<urn:uuid:4362dcf9-1a58-403c-b854-b38e851fefea>\",\"WARC-IP-Address\":\"104.117.39.124\",\"WARC-Target-URI\":\"https://in.mathworks.com/help/textanalytics/ref/htmltree.findelement.html\",\"WARC-Payload-Digest\":\"sha1:TCXYPWIHWIHXSHMGP5EZOWOI6UJHS3AD\",\"WARC-Block-Digest\":\"sha1:G3YKZCDQ5COHV4LPZESHJFVOV5ZQJBQ2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195526237.47_warc_CC-MAIN-20190719115720-20190719141720-00550.warc.gz\"}"}