{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Web scraping" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:12.743707Z", "start_time": "2020-06-25T10:30:12.457375Z" } }, "outputs": [], "source": [ "import requests\n", "from bs4 import BeautifulSoup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:13.221448Z", "start_time": "2020-06-25T10:30:13.205396Z" } }, "outputs": [], "source": [ "def get_soup(session, url):\n", " \"\"\"Convenience function to use a session to get the soup of a webpage.\"\"\"\n", " response = session.get(url)\n", " response.raise_for_status()\n", " return BeautifulSoup(response.text, \"lxml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get all the links on our homepage" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:14.582116Z", "start_time": "2020-06-25T10:30:14.513037Z" } }, "outputs": [], "source": [ "session = requests.Session()\n", "base_url = \"https://www.physik.uzh.ch/~python/python\"\n", "soup = get_soup(session, f\"{base_url}/programme.php\")\n", "links = [a['href'] for a in soup.select(\"a.internal\")]\n", "print(links)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:16.058054Z", "start_time": "2020-06-25T10:30:16.054613Z" } }, "outputs": [], "source": [ "def download(url, file_name):\n", " response = requests.get(url) \n", " response.raise_for_status()\n", " with open(file_name, \"wb\") as f:\n", " f.write(response.content)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:16.765122Z", "start_time": "2020-06-25T10:30:16.577318Z" } }, "outputs": [], "source": [ "for link in links:\n", " soup = get_soup(session, f\"{base_url}/{link}\")\n", " print(soup.title.text.split(\" - \")[-1])\n", " for file_name in [a['href'] for a in soup.select(\"a.download\")]:\n", " url = f\"{base_url}/{link}/{file_name}\"\n", " print(url)\n", " # uncomment this to actually download the files:\n", " # download(url, file_name)\n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subprocess" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:24.872326Z", "start_time": "2020-06-25T10:30:24.869649Z" } }, "outputs": [], "source": [ "import subprocess" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python 3.7+" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:25.463931Z", "start_time": "2020-06-25T10:30:25.378149Z" }, "scrolled": true }, "outputs": [], "source": [ "r = subprocess.run([\"du\", \"-h\", \".\"], capture_output=True)\n", "print(r.stdout)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2020-06-25T10:30:28.663280Z", "start_time": "2020-06-25T10:30:28.653155Z" } }, "outputs": [], "source": [ "subprocess.run([\"cat\"], capture_output=True, input=b\"test\")\n", "print(r.stdout)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.8.10" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 2 }