Skip to content

Commit

Permalink
Merge pull request #936 from agastya3636/main
Browse files Browse the repository at this point in the history
Issue no #928 : Scraping data from geeksforgeeks
  • Loading branch information
nikhil25803 authored May 14, 2024
2 parents a214c14 + 6e8ed3c commit e99fbef
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dev-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1616,3 +1616,16 @@ First create an object of class `Dictionary`.
| `.word_of_the_day_definition()` | Returns the definition of the word of the day. |

---

## Geeksforgeeks

First create an object of class `Geeksforgeeks`.
```python
geeksforgeeks = Geeksforgeeks(user="username")
```

| Methods | Details |
| ------------------------------- | ---------------------------------------------- |
| `.get_profile()` | Returns the user data in json format. |

---
3 changes: 3 additions & 0 deletions src/scrape_up/geeksforgeeks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .geeksforgeeks import Geeksforgeeks

__all__ = ["Geeksforgeeks"]
94 changes: 94 additions & 0 deletions src/scrape_up/geeksforgeeks/geeksforgeeks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import requests
from bs4 import BeautifulSoup


class Geeksforgeeks:
"""
Create an instance of the class `GeeksforGeeks`
```py
gfg = Geeksforgeeks(user="nikhil25803")
gfg.get_profile()
```
| Methods | Details |
| ----------------- | ---------------------------------------------------------------------------------- |
| `.get_profile()` | Returns the user data in json format. |
Response:
```js
{
"username": "22cs3iehq",
"collage_name": "Rajiv Gandhi Institute of Petroleum Technology (RGIPT) Rae Bareli",
"collage_rank": "1",
"score": {
"overall_coding_score": "6085",
"monthly_coding_score": "14"
},
"languages_used": "C++, Javascript, Python, Java, C",
"current_potd_streak": "407/1015",
"total_problem_solved": "1534",
"campus_ambassader": "22cs3iehq"
}
```
"""

def __init__(self, user):
self.user = user

def get_profile(self):
try:
url = f"https://www.geeksforgeeks.org/user/{self.user}/"
headers = {"User-Agent": "scrapeup"}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
main_info = soup.find("div", class_="AuthLayout_head_content__ql3r2")
user_data = []

username = main_info.find(
"div",
class_="profilePicSection_head_userHandleAndFollowBtnContainer_userHandle__p7sDO",
).text
collage_rank = main_info.find(
"span", class_="profilePicSection_head_userRankContainer_rank__abngM"
).text
collage = main_info.find(
"div", class_="educationDetails_head_left--text__tgi9I"
).text
languages = main_info.find(
"div", class_="educationDetails_head_right--text__lLOHI"
).text
campus_ambaasder = soup.find(
"a", class_="basicUserDetails_head_CA--text__IoHEU"
).text
current_potd_streak = main_info.find(
"div", class_="circularProgressBar_head_mid_streakCnt__MFOF1 tooltipped"
).text
score = main_info.find_all(
"div", class_="scoreCard_head_card_left--score__pC6ZA"
)
overall_coding_score = score[0].text
total_problem_solved = score[1].text
monthly_coding_score = score[2].text

user_data = {
"username": username,
"collage_name": collage,
"collage_rank": collage_rank,
"score": {
"overall_coding_score": overall_coding_score,
"monthly_coding_score": monthly_coding_score,
},
"languages_used": languages,
"current_potd_streak": current_potd_streak,
"total_problem_solved": total_problem_solved,
"campus_ambassader": campus_ambaasder,
}

return user_data
except:
return None


gfg = Geeksforgeeks(user="nikhil25803")
print(gfg.get_profile())

0 comments on commit e99fbef

Please sign in to comment.