|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 練習\n", |
| 8 | + "\n", |
| 9 | + "- 觀察 http://exam.lib.ntu.edu.tw/graduate 並撰寫爬蟲程式\n", |
| 10 | + "- request 附上 User-Agent 資訊\n", |
| 11 | + "- 下載頁面上所有 pdf 考古題檔案" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": { |
| 18 | + "collapsed": true |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import requests\n", |
| 23 | + "import re\n", |
| 24 | + "import os\n", |
| 25 | + "\n", |
| 26 | + "from PIL import Image\n", |
| 27 | + "from bs4 import BeautifulSoup\n", |
| 28 | + "from fake_useragent import UserAgent\n", |
| 29 | + "from urllib.parse import urljoin\n", |
| 30 | + "from pprint import pprint\n", |
| 31 | + "\n", |
| 32 | + "url = 'http://exam.lib.ntu.edu.tw/graduate'" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 2, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "fu = UserAgent()\n", |
| 42 | + "headers = {'User-Agent': fu.random}\n", |
| 43 | + "resp = requests.get(url, headers=headers)\n", |
| 44 | + "soup = BeautifulSoup(resp.text, 'lxml')" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 3, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [ |
| 52 | + { |
| 53 | + "name": "stdout", |
| 54 | + "output_type": "stream", |
| 55 | + "text": [ |
| 56 | + "(1/30) catch the filename 106_graduate_4.pdf\n", |
| 57 | + "(1/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_4.pdf\n", |
| 58 | + "(2/30) catch the filename 106_graduate_6.pdf\n", |
| 59 | + "(2/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_6.pdf\n", |
| 60 | + "(3/30) catch the filename 106_graduate_3.pdf\n", |
| 61 | + "(3/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_3.pdf\n", |
| 62 | + "(4/30) catch the filename 106_graduate_1.pdf\n", |
| 63 | + "(4/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_1.pdf\n", |
| 64 | + "(5/30) catch the filename 106_graduate_2.pdf\n", |
| 65 | + "(5/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_2.pdf\n", |
| 66 | + "(6/30) catch the filename 106_graduate_8.pdf\n", |
| 67 | + "(6/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_8.pdf\n", |
| 68 | + "(7/30) catch the filename 106_graduate_5.pdf\n", |
| 69 | + "(7/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_5.pdf\n", |
| 70 | + "(8/30) catch the filename 106_graduate_10.pdf\n", |
| 71 | + "(8/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_10.pdf\n", |
| 72 | + "(9/30) catch the filename 106_graduate_7.pdf\n", |
| 73 | + "(9/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_7.pdf\n", |
| 74 | + "(10/30) catch the filename 106_graduate_11.pdf\n", |
| 75 | + "(10/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_11.pdf\n", |
| 76 | + "(11/30) catch the filename 106_graduate_13.pdf\n", |
| 77 | + "(11/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_13.pdf\n", |
| 78 | + "(12/30) catch the filename 106_graduate_15.pdf\n", |
| 79 | + "(12/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_15.pdf\n", |
| 80 | + "(13/30) catch the filename 106_graduate_14.pdf\n", |
| 81 | + "(13/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_14.pdf\n", |
| 82 | + "(14/30) catch the filename 106_graduate_8.pdf\n", |
| 83 | + "(14/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_8.pdf\n", |
| 84 | + "(15/30) catch the filename 106_graduate_5.pdf\n", |
| 85 | + "(15/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_5.pdf\n", |
| 86 | + "(16/30) catch the filename 106_graduate_16.pdf\n", |
| 87 | + "(16/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_16.pdf\n", |
| 88 | + "(17/30) catch the filename 106_graduate_17.pdf\n", |
| 89 | + "(17/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_17.pdf\n", |
| 90 | + "(18/30) catch the filename 106_graduate_18.pdf\n", |
| 91 | + "(18/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_18.pdf\n", |
| 92 | + "(19/30) catch the filename 106_graduate_19.pdf\n", |
| 93 | + "(19/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_19.pdf\n", |
| 94 | + "(20/30) catch the filename 106_graduate_17.pdf\n", |
| 95 | + "(20/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_17.pdf\n", |
| 96 | + "(21/30) catch the filename 106_graduate_20.pdf\n", |
| 97 | + "(21/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_20.pdf\n", |
| 98 | + "(22/30) catch the filename 106_graduate_22.pdf\n", |
| 99 | + "(22/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_22.pdf\n", |
| 100 | + "(23/30) catch the filename 106_graduate_21.pdf\n", |
| 101 | + "(23/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_21.pdf\n", |
| 102 | + "(24/30) catch the filename 106_graduate_8.pdf\n", |
| 103 | + "(24/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_8.pdf\n", |
| 104 | + "(25/30) catch the filename 106_graduate_25.pdf\n", |
| 105 | + "(25/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_25.pdf\n", |
| 106 | + "(26/30) catch the filename 106_graduate_23.pdf\n", |
| 107 | + "(26/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_23.pdf\n", |
| 108 | + "(27/30) catch the filename 106_graduate_24.pdf\n", |
| 109 | + "(27/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_24.pdf\n", |
| 110 | + "(28/30) catch the filename 106_graduate_8.pdf\n", |
| 111 | + "(28/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_8.pdf\n", |
| 112 | + "(29/30) catch the filename 106_graduate_26.pdf\n", |
| 113 | + "(29/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_26.pdf\n", |
| 114 | + "(30/30) catch the filename 106_graduate_28.pdf\n", |
| 115 | + "(30/30) save file /home/dirl/github/Python-Crawling-Tutorial/results/106_graduate_28.pdf\n" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "results = os.path.abspath('../results')\n", |
| 121 | + "if not os.path.exists(results):\n", |
| 122 | + " os.makedirs(results)\n", |
| 123 | + "\n", |
| 124 | + "pdfs = soup.find_all('img', class_=re.compile('.*field-icon-application-pdf$'))\n", |
| 125 | + "for i, pdf in enumerate(pdfs):\n", |
| 126 | + " href = pdf.parent['href']\n", |
| 127 | + " abs_href = urljoin(resp.url, href)\n", |
| 128 | + " file_resp = requests.get(abs_href, headers=headers, stream=True)\n", |
| 129 | + " \n", |
| 130 | + " filename = os.path.basename(abs_href)\n", |
| 131 | + " filename = filename.split('&')[0]\n", |
| 132 | + " print('({}/{}) catch the filename {}'.format(i+1, len(pdfs), filename))\n", |
| 133 | + " filename = os.path.join(results, filename)\n", |
| 134 | + "\n", |
| 135 | + " with open(filename, 'wb') as f:\n", |
| 136 | + " for chunk in file_resp.iter_content(2048):\n", |
| 137 | + " f.write(chunk)\n", |
| 138 | + " print('({}/{}) save file {}'.format(i+1, len(pdfs),filename))" |
| 139 | + ] |
| 140 | + } |
| 141 | + ], |
| 142 | + "metadata": { |
| 143 | + "kernelspec": { |
| 144 | + "display_name": "Python 3", |
| 145 | + "language": "python", |
| 146 | + "name": "python3" |
| 147 | + }, |
| 148 | + "language_info": { |
| 149 | + "codemirror_mode": { |
| 150 | + "name": "ipython", |
| 151 | + "version": 3 |
| 152 | + }, |
| 153 | + "file_extension": ".py", |
| 154 | + "mimetype": "text/x-python", |
| 155 | + "name": "python", |
| 156 | + "nbconvert_exporter": "python", |
| 157 | + "pygments_lexer": "ipython3", |
| 158 | + "version": "3.5.2" |
| 159 | + } |
| 160 | + }, |
| 161 | + "nbformat": 4, |
| 162 | + "nbformat_minor": 2 |
| 163 | +} |
0 commit comments