Question:
So I am making a web scraper to get data from an inputted username for Replit Ask. Would I be able to access this data, if so how? (I know you can already find stats on Ask but I think it would still be a cool project).
Repl link:
https://replit.com/@SalladShooter/Replit-Ask-Stat-Tracker#main.py
import requests
from bs4 import BeautifulSoup
username = input("Username of person > ")
URL = f"https://ask.replit.com/u?name={username}&order=likes_received"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
solves_element = soup.find('div', class_='solves-count')
if solves_element is not None:
solves_count = int(solves_element.text)
print(f"Number of solves: {solves_count}")
else:
print("No solves information found.")
There are a handful of endpoints that don’t require an API key. Ex: Discourse API Docs
6 Likes
@not-ethan do you have any code I could use to get the data just so I can see what I need to do? The code I have currently throws an error:
import requests
username = input("Username of person > ")
url = f"https://docs.discourse.org/#tag/Users/operation/adminGetUser/{username}"
response = requests.get(url)
if response.status_code == 200:
user_data = response.json()
solutions_count = user_data['user'].get('solutions_count', 0)
cheers_count = user_data['user'].get('cheers_count', 0)
print(f"Solutions: {solutions_count}")
print(f"Cheers: {cheers_count}")
else:
print(f"Error: Unable to fetch user data. Status code: {response.status_code}")
You dont have the right URL. That link is to the API docs not the endpoint itself. And looking at the endpoint that’s mod only even though it doesn’t say it needs a key (another place where the docs are wrong). Try this one instead Discourse API Docs
5 Likes
@not-ethan when I run it the error it gives is:
JSONDecoderError: Expecting value: line 1 column 1 (char 0)
exit status 1
consider using discourse · PyPI instead of manual requests (you don’t need to pass in the api key/username kwargs)
4 Likes