distrowatch-reviews / extract_reviews_distrowatch.py
bumbledeep's picture
Upload extract_reviews_distrowatch.py
1af59cc verified
# -*- coding: utf-8 -*-
"""extract_reviews_distrowatch.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1RZzzKmKsBL3KzWLmtgQh7cJvIVmQYFSO
"""
from google.colab import drive
drive.mount('/content/drive')
# Commented out IPython magic to ensure Python compatibility.
# %cd /content/drive/MyDrive/Colab_Notebooks/distrowatch NLP
import requests
from pickle import dump as pickle_dump, load as pickle_load
import pandas as pd
try:
import scrapy
except:
ModuleNotFoundError
!uv pip install scrapy --quiet
import scrapy
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
}
url_popularity = 'https://distrowatch.com/dwres.php?resource=popularity'
sel = scrapy.Selector(text=requests.get(url_popularity, headers=headers).text)
distros = list(set(sel.xpath('//td[@class = "phr2"]/a/@href').extract()))
distros[:10]
# with open("distro_names", "wb") as f:
# pickle_dump(distros, f)
# distros[:10]
# with open("distro_names", "rb") as f:
# distros = pickle_load(f)
# distros[:10]
# if we want to focus only on some distros we can provide them in a list
# distros = ['ubuntu','fedora','opensuse','mint','manjaro','debian','kali','endeavour','zorin','parrot','mx']
dfs = []
for distro in distros:
url_rating = f'https://distrowatch.com/dwres.php?resource=ratings&distro={distro}'
try:
response = requests.get(url_rating, headers=headers, timeout=(60, 60))
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"{distro}: failed with error {e}")
continue
sel = scrapy.Selector(text=response.text)
project = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[1]/text()[3]').extract()
version = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[1]/text()[4]').extract()
rating = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[1]/text()[5]').extract()
date_review = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[1]/text()[6]').extract()
votes = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[1]/text()[7]').extract()
reviews = sel.xpath('//td[@class = "News1"]//table[2]//tr/td[2]/text()').extract()
if not reviews:
print(distro + ': no reviews')
continue
# Different paragraphs in the same review are separated by `/r`, but different reviews are separated by `\n`.
# Also, there is another `\n` character at the beggining of the next review.
# In order to split the reviews correctly we can first "glue" all reviews and then use the pattern `'\n\n'` to separate reviews.
reviews = ''.join(reviews).split('\n\n')
df_distro = pd.DataFrame({'date': date_review,
'project': project,
'version': version,
'rating': rating,
'votes': votes,
'review': reviews})
dfs.append(df_distro)
df = pd.concat(dfs)
df = (
df.assign(date=pd.to_datetime(df['date']),
votes=pd.to_numeric(df['votes']),
rating=pd.to_numeric(df['rating']))
.reset_index(drop=True)
)
df.to_csv('distrowatch_2.csv') # 6th April 2025
df.head()
df.groupby('project')['rating'].describe().sort_values(by='count',ascending=False)
df_list = [pd.read_csv(f'distrowatch_{i}.csv', index_col=0,lineterminator='\n') for i in range(1,3)]
full_df = pd.concat(df_list) \
.drop_duplicates() \
.reset_index(drop=True)
full_df['date'] = pd.to_datetime(full_df['date'])
full_df['review'] = full_df['review'].str.strip().str.replace('\r+', ' ', regex=True)
full_df['project'] = full_df['project'].str.strip()
full_df
full_df.info()
full_df.iloc[0,5]
full_df.to_csv('full_distrowatch.csv', index=False)