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

Conteúdo do Curso

Web Scraping with Python (res)

Web Scraping with Python (res)

1. HTML Files and DevTools
2. Beautiful Soup
3. CSS Selectors/XPaths
4. Tables

bookIntro to XPath

In the previous sections, we used a lot of different methods to find needed data without knowing the HTML way to the tag. To extract desired parts of code describing where the elements exactly are (in your HTML structure), you can use XPath.

XPath provides us with simple Python-friendly syntax to navigate through the HTML file. The notation is familiar with folder organization on your computer. For example, in the syntax of the XPathes / means move forward to one generation, like in the directory hierarchy where the same symbol moves us deeper into the folder's content. For instance:

Here we define the path to all div tags in body tags of html tags.

To specify which div from a variety of tags you want, enclose its number in square brackets []:

This path for the following tree will detect the second p of the body (numeration starts from 1):

If you want to detect all p tags everywhere, use //. This symbol is used to pass tags. For example, you want to detect all p blocks but don’t know the whole path to each one:

It also can be used when you know the beginning or part of your path:

The code above directs all p tags in the body tag.

question-icon

You have the HTML tree. Write the path to the second div tag in the body tag and all p tags:

xpath_div = "/html/"
xpath_p = "
p"

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 1
some-alt