|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 13, |
| 6 | + "id": "c9cae81a-2aea-4fac-b3b9-8a25738e2b77", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import requests\n", |
| 11 | + "import xml.etree.ElementTree as ET\n", |
| 12 | + "from html.parser import HTMLParser" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "id": "0dbe8524-a52c-4689-bb92-a835c6141d37", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "id": "c55bc9b1-5592-4cad-933d-91062ef4450e", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "id": "7792f381-240d-4733-8807-adfcf8cd10d5", |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": null, |
| 42 | + "id": "dd9194b4-0a52-4493-974f-d117468d3f08", |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 36, |
| 50 | + "id": "d6885753-934f-49a3-aa33-07b5dacb0504", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "\n", |
| 55 | + "def download_html(url):\n", |
| 56 | + " try:\n", |
| 57 | + " response = requests.get(url)\n", |
| 58 | + " response.raise_for_status() # Check if the request was successful\n", |
| 59 | + " return response.text\n", |
| 60 | + " except requests.exceptions.RequestException as e:\n", |
| 61 | + " print(f\"An error occurred: {e}\")\n", |
| 62 | + " return None\n", |
| 63 | + "\n", |
| 64 | + "\n", |
| 65 | + "class MyHTMLParser(HTMLParser):\n", |
| 66 | + " def __init__(self):\n", |
| 67 | + " super().__init__()\n", |
| 68 | + " self.capture_data = False\n", |
| 69 | + " self.target_class = \"\"\n", |
| 70 | + " self.captured_data = []\n", |
| 71 | + "\n", |
| 72 | + " def handle_starttag(self, tag, attrs):\n", |
| 73 | + " if tag == \"span\":\n", |
| 74 | + " for attr in attrs:\n", |
| 75 | + " if attr[0] == \"class\" and attr[1] == self.target_class:\n", |
| 76 | + " self.capture_data = True\n", |
| 77 | + "\n", |
| 78 | + " def handle_endtag(self, tag):\n", |
| 79 | + " if tag == \"span\" and self.capture_data:\n", |
| 80 | + " self.capture_data = False\n", |
| 81 | + "\n", |
| 82 | + " def handle_data(self, data):\n", |
| 83 | + " if self.capture_data:\n", |
| 84 | + " self.captured_data.append(data)\n", |
| 85 | + "\n", |
| 86 | + " def extract_span_by_class(self, html, class_value):\n", |
| 87 | + " self.target_class = class_value\n", |
| 88 | + " self.captured_data = []\n", |
| 89 | + " self.feed(html)\n", |
| 90 | + " return self.captured_data\n" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 31, |
| 96 | + "id": "37a457e7-fa4f-483c-84dd-bdd03f8a7888", |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "\n", |
| 101 | + "url = \"https://www.example.com\"\n", |
| 102 | + "url = \"https://filesender.aarnet.edu.au/?s=download&token=d6be9395-9fc6-427f-b565-289cb32a16e2\"\n", |
| 103 | + "html_content = download_html(url)" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "id": "2c4637b2-04c8-40ad-8c0c-e2c1bb3f042f", |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 21, |
| 117 | + "id": "8e829e58-0c4f-46a7-a876-6051cbe3c7a1", |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 44, |
| 125 | + "id": "24c488c9-ba2a-403c-8a82-99c5ee35223a", |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "\n", |
| 130 | + "parser = MyHTMLParser()\n", |
| 131 | + "directlinks = parser.extract_span_by_class(html_content, \"directlink\")\n", |
| 132 | + "directlinks = [x.split(\"Direct Link: \")[1].strip() for x in directlinks]\n", |
| 133 | + "token = url.split(\"&token=\")[1]\n", |
| 134 | + "fileids = [x.split(\"&files_ids=\")[1] for x in directlinks]\n" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "id": "60975e26-2bfc-4fd1-bcd1-dd2478be173f", |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "id": "d973a5e6-c9b4-4e7d-80f1-6be0557c92a9", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "id": "9d855a73-8007-4a75-b5e7-5ca7aae01c3b", |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [], |
| 159 | + "source": [] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "id": "ba26a9ca-02db-4041-99c3-264982b238b4", |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [] |
| 168 | + } |
| 169 | + ], |
| 170 | + "metadata": { |
| 171 | + "kernelspec": { |
| 172 | + "display_name": "Python 3 (ipykernel)", |
| 173 | + "language": "python", |
| 174 | + "name": "python3" |
| 175 | + }, |
| 176 | + "language_info": { |
| 177 | + "codemirror_mode": { |
| 178 | + "name": "ipython", |
| 179 | + "version": 3 |
| 180 | + }, |
| 181 | + "file_extension": ".py", |
| 182 | + "mimetype": "text/x-python", |
| 183 | + "name": "python", |
| 184 | + "nbconvert_exporter": "python", |
| 185 | + "pygments_lexer": "ipython3", |
| 186 | + "version": "3.10.14" |
| 187 | + } |
| 188 | + }, |
| 189 | + "nbformat": 4, |
| 190 | + "nbformat_minor": 5 |
| 191 | +} |
0 commit comments