Element by class returns None

Viewed 45

I'm trying to simply get the number of users on a subredit.
When I open the HTML, I can see it.

<div class="_3XFx6CfPlg-4Usgxm0gK8R">55.3k</div>

I wrote a bit of python code to try and get the number:

import requests
from bs4 import BeautifulSoup

url = "https://www.reddit.com/r/TowerofGod/"

response = requests.get(url)

soup = BeautifulSoup(response.text, "html.parser")

regulars = soup.find("div", class_ = "_3XFx6CfPlg-4Usgxm0gK8R")

print(regulars)

But I only get None as a response. What am I doing wrong?

1 Answers

The class is dynamically generated each time, so each time it will change and you will not be able to get it by that exact string.

How to do it?

Reddit offers you a json version of the page! Only putting a .json at the end of the url, it will serve you with all the data of the subreddit, even posts!

example for the url:

https://www.reddit.com/r/TowerofGod.json

if you see inside the data, you will find the element: subreddit_subscribers 55389 (its a little inside of children and the data, but hey, the exact same as the sub have!)

in the json the steps to get to the element:

response -> children -> 0 -> data -> subreddit_subscribers

Related