Skip to content

Instantly share code, notes, and snippets.

@ksqsf
Created August 6, 2017 17:36
Show Gist options
  • Save ksqsf/f1e2837ac9c13aa99073e20aef4fecb7 to your computer and use it in GitHub Desktop.
Save ksqsf/f1e2837ac9c13aa99073e20aef4fecb7 to your computer and use it in GitHub Desktop.
Plot the number of notices on the official site of Arch Linux
import urllib.request
from datetime import datetime
from collections import Counter
from threading import Thread
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
base_url_fmt = "https://www.archlinux.org/news/?page=%d"
max_page = 13
def fetch_page(page):
print('Fetching page', page)
with urllib.request.urlopen(base_url_fmt % page) as response:
return response.read()
def main():
years = []
# Gather information
for page in range(1, max_page+1):
html = fetch_page(page)
soup = BeautifulSoup(html, 'html5lib')
years.extend(datetime.strptime(tr.td.contents[0], '%Y-%m-%d').date().year
for tr in soup.tbody.find_all('tr'))
# How many years?
max_year = max(years)
min_year = min(years)
# Each year, how many times?
cnt = Counter(years)
# Plot!
x = list(range(min_year, max_year+1))
y = [cnt[year] for year in x]
plt.xlabel('Year')
plt.ylabel('# Notices')
plt.bar(x, y)
plt.show()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment