Зміст курсу
Web Scraping with Python (res)
Web Scraping with Python (res)
Attributes in XPath
You can also navigate through the HTML file using attributes as the parameters! To do this, print @
before the type of your attribute (class
, id
, or href
) and the equal sign to find which string the attribute is equal to. Don’t forget to enclose the expression in square brackets.
For instance:
The code above defines the path to all div
tags of the body
tag, where the id
is id1
.
You can use searching by attribute at any place of the path you want:
Here we select the third p
tag of the previously found div
tag by the id
attribute.
To direct all child elements of the tag, you can use an asterisk *
(like in SQL). For example, the path to all tags of the body
tag:
For example, if you want to find all elements, which class
is equal to class1
:
Be careful with quotes! It’s a good rule to define the path with one type of quote (double) and the attributes with another one (single). Or vice versa.
Дякуємо за ваш відгук!