Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Attributes in XPath | CSS Selectors/XPaths
Web Scraping with Python (res)

bookAttributes 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:

xpath = "/html/body/div[@id = 'id1']"

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:

xpath = "/html/body/div[@id = 'id1']/p[3]"

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:

xpath = "/html/body/*"

For example, if you want to find all elements, which class is equal to class1:

xpath = "//*[@class = '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.

1. Write the path to all tags of the body tag:

2. You want to find all children of all p tags whose class is class0. Choose the correct path:

question-icon

Write the path to all tags of the body tag:

xpath = "/html"

Click or drag`n`drop items and fill in the blanks

question mark

You want to find all children of all p tags whose class is class0. Choose the correct path:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Ask me questions about this topic

Summarize this chapter

Show real-world examples

Awesome!

Completion rate improved to 4.76

bookAttributes in XPath

Swipe to show menu

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:

xpath = "/html/body/div[@id = 'id1']"

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:

xpath = "/html/body/div[@id = 'id1']/p[3]"

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:

xpath = "/html/body/*"

For example, if you want to find all elements, which class is equal to class1:

xpath = "//*[@class = '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.

1. Write the path to all tags of the body tag:

2. You want to find all children of all p tags whose class is class0. Choose the correct path:

question-icon

Write the path to all tags of the body tag:

xpath = "/html"

Click or drag`n`drop items and fill in the blanks

question mark

You want to find all children of all p tags whose class is class0. Choose the correct path:

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt