Refering to two classes inside an Html tag using BeautifulSoup 4

Viewed 97

hello im having an issue trying to get my dom element of a specific tag that has two class let's have this example :

<a href="website" class="tab_item   app_impression_tracked >
</a>

I used as a code for finding elements inside my <a> tag

containers = page_soup.find_all("a", {"class": "tab_item.app_impressions_tracked"})

and as a result I get an empty variable which means it's not working, do you guys have any other alternative that can help me ? even the css selector didnt solve the problem

2 Answers

The page is loaded dynamically, therefore if we call soup.prettify(), you see that all the desired output is under the class tab_item which includes tags that we don't want, and not under the class tab_item app_impression_tracked.

A different approach would be to use a CSS Selector to find all links under the NewReleasesRows ID (<div id="NewReleasesRows">).

To use a CSS Selector use the select() method instead of find_all() (in your example you have used find_all() instead of select())

import requests
from bs4 import BeautifulSoup

URL = "https://store.steampowered.com/tags/fr/RPG/"
soup = BeautifulSoup(requests.get(URL).content, "html.parser")

for tag in soup.select("#NewReleasesRows > a"):
    print(tag["href"])

Output:

https://store.steampowered.com/app/1433420/Hero_by_Chance/?snr=1_241_4_rpg_103
https://store.steampowered.com/app/1235140/Yakuza_Like_a_Dragon/?snr=1_241_4_rpg_103
https://store.steampowered.com/app/1445440/Blacksmith_of_the_Sand_Kingdom/?snr=1_241_4_rpg_103
...And on...

I would use css selectors for this:

# You can chain classes together, css will match objects which satisfy both classes
# the added [href] specifies element must have an href
my_links = soup.select('a.tab_item.app_impression_tracked[href]')

# Get the hrefs for each one:
my_links_href = [x.get("href") for x in my_links]
Related