Select multiple HTML types
As you have seen in the video, CSS can be used to style a web page. In the most basic form, this happens via type selectors, where styles are defined for and applied to all HTML elements of a certain type. In turn, you can also use type selectors to scrape pages for specific HTML elements.
As demonstrated in the video, you can also combine multiple type selectors via a comma, i.e. with html_elements("type1, type2")
. This selects all elements that have type1
or type2
.
Have a look at the following HTML:
<html>
<body>
<div>Python is perfect for programming.</div>
<p>Still, R might be better suited for data analysis.</p>
<small>(And has prettier charts, too.)</small>
</body>
</html>
The raw HTML code is provided to you in the variable languages_raw_html
.
This exercise is part of the course
Web Scraping in R
Exercise instructions
- Read in
languages_raw_html
. - Using the method shown above, select all
div
andp
elements in this HTML.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read in the HTML
languages_html <- ___
# Select the div and p tags and print their text
languages_html %>%
___ %>%
html_text()