Skip to content

Instantly share code, notes, and snippets.

@stucka
Created April 19, 2018 19:04
Show Gist options
  • Select an option

  • Save stucka/48c4f62f17df2101aca9ce8a153eb735 to your computer and use it in GitHub Desktop.

Select an option

Save stucka/48c4f62f17df2101aca9ce8a153eb735 to your computer and use it in GitHub Desktop.
UWSP agenda scraper
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from selenium import webdriver\n",
"from selenium.webdriver.common.keys import Keys\n",
"from pyquery import PyQuery as pq"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"hosturl = \"https://committees.uwsp.edu/facsen/agendas/SitePages/Home.aspx\""
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"phantomJSlocation = r\"c:\\helpers\\phantomjs.exe\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"browser = webdriver.PhantomJS(executable_path=phantomJSlocation)\n",
"browser.get(hosturl)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"html = browser.page_source"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"tables = pq(html)(\"table\")"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"global mytable\n",
"for i, table in enumerate(tables):\n",
" # print(\"Table \" + str(i) + \" found. Looking.\")\n",
" if pq(table).attr(\"summary\") == \"Current Academic Year \":\n",
" mytable = tables[i]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"output = \"<html><head></head><body><table>\" + pq(mytable).html() + \"</table></body></html>\""
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"with open(\"output.html\", \"w\") as f:\n",
" f.write(output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment