-
Notifications
You must be signed in to change notification settings - Fork 17
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 #416 from danieldotnl/extract_html
New feature to scrape and extract raw html
- Loading branch information
Showing
6 changed files
with
146 additions
and
22 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
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,108 @@ | ||
"""Tests for scraper class.""" | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.template import Template | ||
|
||
from custom_components.multiscrape.const import DEFAULT_SEPARATOR | ||
from custom_components.multiscrape.scraper import Scraper | ||
from custom_components.multiscrape.selector import Selector | ||
|
||
|
||
async def test_scrape_extract_text(hass: HomeAssistant) -> None: | ||
"""Test scraping and extract text method.""" | ||
scraper = Scraper("test_scraper", hass, None, "lxml", DEFAULT_SEPARATOR) | ||
await scraper.set_content( | ||
"<div class='current-version material-card text'>" | ||
"<h1>Current Version: 2024.8.3</h1>Released: <span class='release-date'>January 17, 2022</span>" | ||
"<div class='links' style='links'><a href='/latest-release-notes/'>Release notes</a>" | ||
"</div>" | ||
"</div>" | ||
"<template>Trying to get</template>" | ||
"<div class='current-time'>" | ||
"<h1>Current Time:</h1><span class='utc-time'>2022-12-22T13:15:30Z</span>" | ||
"</div>" | ||
) | ||
|
||
selector_conf = { | ||
"select": Template(".current-version h1", hass), | ||
"extract": "text", | ||
} | ||
|
||
selector = Selector(hass, selector_conf) | ||
value = scraper.scrape(selector, "test_sensor") | ||
assert value == "Current Version: 2024.8.3" | ||
|
||
async def test_scrape_extract_content(hass: HomeAssistant) -> None: | ||
"""Test scraping and extract contents method.""" | ||
scraper = Scraper("test_scraper", hass, None, "lxml", DEFAULT_SEPARATOR) | ||
await scraper.set_content( | ||
"<div class='current-version material-card text'>" | ||
"<h1>Current Version: 2024.8.3</h1>Released: <span class='release-date'>January 17, 2022</span>" | ||
"<div class='links' style='links'><a href='/latest-release-notes/'>Release notes</a>" | ||
"</div>" | ||
"</div>" | ||
"<template>Trying to get</template>" | ||
"<div class='current-time'>" | ||
"<h1>Current Time:</h1><span class='utc-time'>2022-12-22T13:15:30Z</span>" | ||
"</div>" | ||
) | ||
|
||
selector_conf = { | ||
"select": Template(".links", hass), | ||
"extract": "content", | ||
} | ||
|
||
selector = Selector(hass, selector_conf) | ||
value = scraper.scrape(selector, "test_sensor") | ||
assert value == '<a href="/latest-release-notes/">Release notes</a>' | ||
|
||
async def test_scrape_extract_tag(hass: HomeAssistant) -> None: | ||
"""Test scraping and extract tag method.""" | ||
scraper = Scraper("test_scraper", hass, None, "lxml", DEFAULT_SEPARATOR) | ||
await scraper.set_content( | ||
"<div class='current-version material-card text'>" | ||
"<h1>Current Version: 2024.8.3</h1>Released: <span class='release-date'>January 17, 2022</span>" | ||
"<div class='links' style='links'><a href='/latest-release-notes/'>Release notes</a>" | ||
"</div>" | ||
"</div>" | ||
"<template>Trying to get</template>" | ||
"<div class='current-time'>" | ||
"<h1>Current Time:</h1><span class='utc-time'>2022-12-22T13:15:30Z</span>" | ||
"</div>" | ||
) | ||
|
||
selector_conf = { | ||
"select": Template(".links", hass), | ||
"extract": "tag", | ||
} | ||
|
||
selector = Selector(hass, selector_conf) | ||
value = scraper.scrape(selector, "test_sensor") | ||
assert value == '<div class="links" style="links"><a href="/latest-release-notes/">Release notes</a></div>' | ||
|
||
async def test_scrape_extract_attribute(hass: HomeAssistant) -> None: | ||
"""Test scraping and extract an HTML attribute value.""" | ||
scraper = Scraper("test_scraper", hass, None, "lxml", DEFAULT_SEPARATOR) | ||
await scraper.set_content( | ||
"<div class='current-version material-card text'>" | ||
"<h1>Current Version: 2024.8.3</h1>Released: <span class='release-date'>January 17, 2022</span>" | ||
"<div class='links' style='links'><a href='/latest-release-notes/'>Release notes</a>" | ||
"</div>" | ||
"</div>" | ||
"<template>Trying to get</template>" | ||
"<div class='current-time'>" | ||
"<h1>Current Time:</h1><span class='utc-time'>2022-12-22T13:15:30Z</span>" | ||
"</div>" | ||
) | ||
|
||
selector_conf = { | ||
"select": Template(".links a", hass), | ||
"attribute": "href", | ||
} | ||
|
||
selector = Selector(hass, selector_conf) | ||
value = scraper.scrape(selector, "test_sensor") | ||
assert value == '/latest-release-notes/' | ||
|
||
|
||
|
||
|