BeautifulSoup parser.select symbols

Screen Link:
https://app.dataquest.io/m/54/web-scraping/8/using-css-selectors

Someone please explain to me why the “.” and “#” are used after the parser.select (like below).
It is not in the instructions.

first_outer_text = parser.select(".outer-text")[0].text
second_text = parser.select("#second")[0].text
1 Like

Hi @LuukvanVliet,

. and # are CSS selectors. .name is used to select HTML elements by class name and #name is used to select an HTML element by its id.

You can read more about it here:
https://htmldog.com/guides/css/intermediate/classid/

parser.select(".outer-text") will select all the HTML elements with the classname outer-text. [0] will select the first element. And .text will return the content inside that element.

In the same way, parser.select("#second") will select the HTML element with an id called second.

Best,
Sahil

2 Likes

This whole lesson should be looked at @Sahil/@Bruno. Too much guess-work and not enough instructions.
Thank you.

1 Like

Yeah, this is very old content that isn’t up to our current standards. We have someone working on a completely new course on these topics.

3 Likes

please what is the name/ purpose of [0]?
i understand its use for indexing, where I want to get the first response.
however, in a case like #second where there is only one response with that id, why is it still necessary?

also, what if I want to print out all the responses as a list without specifying any index?

1 Like

Hi @rayolufadeju,

An index is necessary because .select() method returns a list even if there is only one item to be selected.

What if I want to print out all the responses as a list without specifying any index?

There is no problem with that. However, if you want to work with an element like getting its text value, then you have to either use index or loop through the list and call the .text attribute.

Best,
Sahil

1 Like