|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 練習\n", |
| 8 | + "\n", |
| 9 | + "- 觀察 https://afuntw.github.io/Test-Crawling-Website/pages/portfolio/index.html 並撰寫爬蟲程式\n", |
| 10 | + "- 下載 5 張圖片\n", |
| 11 | + "- 以正確的圖片格式存檔" |
| 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 os\n", |
| 24 | + "\n", |
| 25 | + "from PIL import Image\n", |
| 26 | + "from bs4 import BeautifulSoup\n", |
| 27 | + "from pprint import pprint\n", |
| 28 | + "\n", |
| 29 | + "url = 'https://afuntw.github.io/Test-Crawling-Website/pages/portfolio/index.html'" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 2, |
| 35 | + "metadata": { |
| 36 | + "collapsed": true |
| 37 | + }, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "resp = requests.get(url)\n", |
| 41 | + "soup = BeautifulSoup(resp.text, 'lxml')" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": 3, |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "imgs = soup.find_all('img')\n", |
| 51 | + "imgs = [i['src'] for i in imgs]" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 4, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "name": "stdout", |
| 61 | + "output_type": "stream", |
| 62 | + "text": [ |
| 63 | + "catch the filename XgXT3Va.png and the real format is JPEG\n", |
| 64 | + "catch the real filename XgXT3Va.jpeg\n", |
| 65 | + "save image at /home/dirl/github/Python-Crawling-Tutorial/results/XgXT3Va.jpeg\n", |
| 66 | + "catch the filename Q3bkStv.png and the real format is PNG\n", |
| 67 | + "catch the real filename Q3bkStv.png\n", |
| 68 | + "save image at /home/dirl/github/Python-Crawling-Tutorial/results/Q3bkStv.png\n", |
| 69 | + "catch the filename IDPxvSl.jpg and the real format is PNG\n", |
| 70 | + "catch the real filename IDPxvSl.png\n", |
| 71 | + "save image at /home/dirl/github/Python-Crawling-Tutorial/results/IDPxvSl.png\n", |
| 72 | + "catch the filename ZEhBDs6.png and the real format is PNG\n", |
| 73 | + "catch the real filename ZEhBDs6.png\n", |
| 74 | + "save image at /home/dirl/github/Python-Crawling-Tutorial/results/ZEhBDs6.png\n", |
| 75 | + "catch the filename UKxK6FZ.gif and the real format is PNG\n", |
| 76 | + "catch the real filename UKxK6FZ.png\n", |
| 77 | + "save image at /home/dirl/github/Python-Crawling-Tutorial/results/UKxK6FZ.png\n" |
| 78 | + ] |
| 79 | + } |
| 80 | + ], |
| 81 | + "source": [ |
| 82 | + "results = os.path.abspath('../results')\n", |
| 83 | + "if not os.path.exists(results):\n", |
| 84 | + " os.makedirs(results)\n", |
| 85 | + "\n", |
| 86 | + "for i in imgs:\n", |
| 87 | + " img_resp = requests.get(i, stream=True)\n", |
| 88 | + " image = Image.open(img_resp.raw)\n", |
| 89 | + " filename = os.path.basename(i)\n", |
| 90 | + " print('catch the filename {} and the real format is {}'.format(filename, image.format))\n", |
| 91 | + " \n", |
| 92 | + " real_filename = '{}.{}'.format(\n", |
| 93 | + " filename.split('.')[0],\n", |
| 94 | + " image.format.lower()\n", |
| 95 | + " )\n", |
| 96 | + " save_filename = os.path.join(results, real_filename)\n", |
| 97 | + " print('catch the real filename {}'.format(real_filename))\n", |
| 98 | + " \n", |
| 99 | + " image.save(save_filename)\n", |
| 100 | + " print('save image at {}'.format(save_filename))" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "metadata": { |
| 105 | + "kernelspec": { |
| 106 | + "display_name": "Python 3", |
| 107 | + "language": "python", |
| 108 | + "name": "python3" |
| 109 | + }, |
| 110 | + "language_info": { |
| 111 | + "codemirror_mode": { |
| 112 | + "name": "ipython", |
| 113 | + "version": 3 |
| 114 | + }, |
| 115 | + "file_extension": ".py", |
| 116 | + "mimetype": "text/x-python", |
| 117 | + "name": "python", |
| 118 | + "nbconvert_exporter": "python", |
| 119 | + "pygments_lexer": "ipython3", |
| 120 | + "version": "3.5.2" |
| 121 | + } |
| 122 | + }, |
| 123 | + "nbformat": 4, |
| 124 | + "nbformat_minor": 2 |
| 125 | +} |
0 commit comments