Skip to content

Instantly share code, notes, and snippets.

@catherinedevlin
Last active December 17, 2015 16:09
Show Gist options
  • Save catherinedevlin/5636516 to your computer and use it in GitHub Desktop.
Save catherinedevlin/5636516 to your computer and use it in GitHub Desktop.
Count of views of PyOhio 2012 videos at pyvideo.org
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "video_count"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import requests\n",
"import bs4\n",
"import re"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mainpage = requests.get('http://pyvideo.org/category/22/pyohio-2012')\n",
"mainsoup = bs4.BeautifulSoup(mainpage.content)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"linkpattern = re.compile('/video/')\n",
"youtubepattern = re.compile('www.youtube.com/watch')\n",
"video_urls = dict()\n",
"for link in mainsoup.findAll('a', href=linkpattern):\n",
" video_urls['http://pyvideo.org' + link['href']] = None\n",
"for video_url in video_urls:\n",
" video_page = requests.get(video_url)\n",
" video_soup = bs4.BeautifulSoup(video_page.content)\n",
" youtube_url = video_soup.find('a', href=youtubepattern)['href']\n",
" youtube_page = requests.get(youtube_url)\n",
" youtube_soup = bs4.BeautifulSoup(youtube_page.content)\n",
" view_span = youtube_soup.find('span', class_=\"watch-view-count \")\n",
" views = view_span.text.strip().split()[0]\n",
" video_urls[video_url] = views"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"video_urls\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"{u'http://pyvideo.org/video/1352/lightning-talks-0-1': u'323',\n",
" u'http://pyvideo.org/video/1353/you-used-python-for-what': u'549',\n",
" u'http://pyvideo.org/video/1354/starting-your-project-right-setup-and-automation': u'582',\n",
" u'http://pyvideo.org/video/1355/video-production-strategies-using-image-sequences': u'144',\n",
" u'http://pyvideo.org/video/1356/python-101102': u'541',\n",
" u'http://pyvideo.org/video/1357/devops-or-how-i-learned-to-stop-worrying-and-lov': u'175',\n",
" u'http://pyvideo.org/video/1358/drive-in-double-header-datetimes-and-log-analysi': u'73',\n",
" u'http://pyvideo.org/video/1359/coding-for-everyone': u'285',\n",
" u'http://pyvideo.org/video/1360/really-good-logging': u'570',\n",
" u'http://pyvideo.org/video/1361/develop-games-with-panda3d-and-python': u'2,816',\n",
" u'http://pyvideo.org/video/1362/leo-a-paradigm-shifting-ide': u'641',\n",
" u'http://pyvideo.org/video/1363/does-python-have-the-secret-sauce-to-be-web-scale': u'291',\n",
" u'http://pyvideo.org/video/1364/an-introduction-to-tkinter': u'2,046',\n",
" u'http://pyvideo.org/video/1365/family-project-three-keys-and-a-boss': u'111',\n",
" u'http://pyvideo.org/video/1366/python-testing-fundamentals': u'1,205',\n",
" u'http://pyvideo.org/video/1367/an-introduction-to-the-zodb': u'284',\n",
" u'http://pyvideo.org/video/1368/using-python-on-android': u'1,417',\n",
" u'http://pyvideo.org/video/1369/python-design-patterns-1': u'3,254',\n",
" u'http://pyvideo.org/video/1370/python-for-educators': u'175',\n",
" u'http://pyvideo.org/video/1371/learn-python-tdd-style-with-python-koans': u'1,537',\n",
" u'http://pyvideo.org/video/1372/exploring-python-code-objects': u'471',\n",
" u'http://pyvideo.org/video/1373/fun-observational-science-with-python-and-a-webca': u'217',\n",
" u'http://pyvideo.org/video/1374/python-for-humans': u'731',\n",
" u'http://pyvideo.org/video/1375/beyond-the-pil-alternative-solutions-for-working': u'665',\n",
" u'http://pyvideo.org/video/1376/effective-django': u'1,172',\n",
" u'http://pyvideo.org/video/1377/django-cms-friends-dont-let-friends-use-drupal': u'4,984',\n",
" u'http://pyvideo.org/video/1378/deleting-code-is-hard-and-you-should-do-it': u'1,630'}"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for url in video_urls:\n",
" views = int(video_urls[url].replace(',',''))\n",
" video_urls[url] = views"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sum(v for v in video_urls.values())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 11,
"text": [
"26889"
]
}
],
"prompt_number": 11
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment