|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Headless scraping using Selenium" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "# Install Selenium (needs to be executed only once)\n", |
| 17 | + "!pip install selenium\n", |
| 18 | + "\n", |
| 19 | + "# Download the selenium driver for your browser and machine from\n", |
| 20 | + "# https://selenium-python.readthedocs.io/installation.html#drivers\n", |
| 21 | + "# and add it to this local folder:\n", |
| 22 | + "import os\n", |
| 23 | + "import sys\n", |
| 24 | + "os.path.dirname(sys.executable)" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "# Imports:\n", |
| 34 | + "import time # for sleeping between multiple requests\n", |
| 35 | + "from selenium import webdriver" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": null, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "#### Documentation:\n", |
| 45 | + "- [Requests.py](http://docs.python-requests.org)\n", |
| 46 | + "- [Beautifulsoup.py](https://www.crummy.com/software/BeautifulSoup/bs4/doc/)\n", |
| 47 | + "- [Selenium with python](https://selenium-python.readthedocs.io/)\n", |
| 48 | + "- [Locating elements](https://selenium-python.readthedocs.io/locating-elements.html#locating-elements)" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "markdown", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "## Headless request to price list (Chrome):" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "options = webdriver.ChromeOptions()\n", |
| 65 | + "options.headless = True\n", |
| 66 | + "options.add_argument('user-agent=scrapingCourseBot')\n", |
| 67 | + "\n", |
| 68 | + "browser = webdriver.Chrome(options=options)\n", |
| 69 | + "browser.get('http://testing-ground.webscraping.pro/price-list-1.html')\n", |
| 70 | + "\n", |
| 71 | + "# Retrieve first item name:\n", |
| 72 | + "elem = browser.find_element_by_css_selector('div.name')\n", |
| 73 | + "print(elem.text)\n", |
| 74 | + "\n", |
| 75 | + "browser.close()" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "metadata": {}, |
| 81 | + "source": [ |
| 82 | + "## Headless request to price list (Firefox):" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "from selenium.webdriver.firefox.options import Options\n", |
| 92 | + "profile = webdriver.FirefoxProfile()\n", |
| 93 | + "profile.set_preference(\"general.useragent.override\", \"scrapingCourseBot\")\n", |
| 94 | + "\n", |
| 95 | + "options = Options()\n", |
| 96 | + "options.headless = True\n", |
| 97 | + "\n", |
| 98 | + "browser = webdriver.Firefox(profile, options=options)\n", |
| 99 | + "browser.get('http://testing-ground.webscraping.pro/price-list-1.html')\n", |
| 100 | + "\n", |
| 101 | + "# Retrieve first item name:\n", |
| 102 | + "elem = browser.find_element_by_css_selector('div.name')\n", |
| 103 | + "print(elem.text)\n", |
| 104 | + "\n", |
| 105 | + "browser.close()" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "## Headless getting Olympic winter games (Chrome): " |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "options = webdriver.ChromeOptions()\n", |
| 122 | + "options.headless = True\n", |
| 123 | + "options.add_argument('user-agent=scrapingCourseBot')\n", |
| 124 | + "\n", |
| 125 | + "browser = webdriver.Chrome(options=options)\n", |
| 126 | + "browser.get('https://olympics.com/en/sports/winter-olympics')\n", |
| 127 | + "\n", |
| 128 | + "# Retrieve first item name:\n", |
| 129 | + "elements = browser.find_elements_by_css_selector('div.-sportlist h1.article--title')\n", |
| 130 | + "for e in elements:\n", |
| 131 | + " print(e.text)\n", |
| 132 | + "\n", |
| 133 | + "browser.close()" |
| 134 | + ] |
| 135 | + } |
| 136 | + ], |
| 137 | + "metadata": { |
| 138 | + "kernelspec": { |
| 139 | + "display_name": "Python 3", |
| 140 | + "language": "python", |
| 141 | + "name": "python3" |
| 142 | + }, |
| 143 | + "language_info": { |
| 144 | + "codemirror_mode": { |
| 145 | + "name": "ipython", |
| 146 | + "version": 3 |
| 147 | + }, |
| 148 | + "file_extension": ".py", |
| 149 | + "mimetype": "text/x-python", |
| 150 | + "name": "python", |
| 151 | + "nbconvert_exporter": "python", |
| 152 | + "pygments_lexer": "ipython3", |
| 153 | + "version": "3.8.5" |
| 154 | + } |
| 155 | + }, |
| 156 | + "nbformat": 4, |
| 157 | + "nbformat_minor": 4 |
| 158 | +} |
0 commit comments