-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_trending.py
More file actions
29 lines (23 loc) · 859 Bytes
/
github_trending.py
File metadata and controls
29 lines (23 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
from bs4 import BeautifulSoup
import csv
# URL to scrape
url = "https://github.com/trending"
# Fetch the webpage
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# Extract top 5 repositories
repos = soup.find_all('article', class_='Box-row')[:5]
# Prepare data
repo_data = []
for repo in repos:
repo_name = repo.h2.a.text.strip().replace('\n', '').replace(' ', '')
repo_link = "https://github.com" + repo.h2.a['href'].strip()
repo_data.append([repo_name, repo_link])
# Write to CSV
csv_filename = "trending_repos.csv"
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(["Repository Name", "Link"])
writer.writerows(repo_data)
print(f"Top 5 trending repositories saved to {csv_filename}")