| |
| """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') |
|
|
| |
| |
|
|
| 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] |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| 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 |
|
|
| |
| |
| |
| 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') |
|
|
| 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) |
|
|
|
|
|
|
|
|