Movatterモバイル変換


[0]ホーム

URL:


Python Web Scraping Tutorial

Python Modules for Web Scraping



In this chapter, let us learn various Python modules that we can use for web scraping.

Python Development Environments using virtualenv

Virtualenv is a tool to create isolated Python environments. With the help of virtualenv, we can create a folder that contains all necessary executables to use the packages that our Python project requires. It also allows us to add and modify Python modules without access to the global installation.

You can use the following command to installvirtualenv

(base) D:\ProgramData>pip install virtualenvCollecting virtualenv   Downloadinghttps://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl(1.9MB)   100% || 1.9MB 86kB/sInstalling collected packages: virtualenvSuccessfully installed virtualenv-16.0.0

Now, we need to create a directory which will represent the project with the help of following command −

(base) D:\ProgramData>mkdir webscrap

Now, enter into that directory with the help of this following command −

(base) D:\ProgramData>cd webscrap

Now, we need to initialize virtual environment folder of our choice as follows −

(base) D:\ProgramData\webscrap>virtualenv webscUsing base prefix 'd:\\programdata'New python executable in D:\ProgramData\webscrap\websc\Scripts\python.exeInstalling setuptools, pip, wheel...done.

Now, activate the virtual environment with the command given below. Once successfully activated, you will see the name of it on the left hand side in brackets.

(base) D:\ProgramData\webscrap>websc\scripts\activate

We can install any module in this environment as follows −

(websc) (base) D:\ProgramData\webscrap>pip install requestsCollecting requests   Downloadinghttps://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)   100% || 92kB 148kB/sCollecting chardet<3.1.0,>=3.0.2 (from requests)   Downloadinghttps://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)   100% || 143kB 369kB/sCollecting certifi>=2017.4.17 (from requests)   Downloadinghttps://files.pythonhosted.org/packages/df/f7/04fee6ac349e915b82171f8e23cee63644d83663b34c539f7a09aed18f9e/certifi-2018.8.24-py2.py3-none-any.whl(147kB)   100% || 153kB 527kB/sCollecting urllib3<1.24,>=1.21.1 (from requests)   Downloadinghttps://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)   100% || 143kB 517kB/sCollecting idna<2.8,>=2.5 (from requests)   Downloadinghttps://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)   100% || 61kB 339kB/sInstalling collected packages: chardet, certifi, urllib3, idna, requestsSuccessfully installed certifi-2018.8.24 chardet-3.0.4 idna-2.7 requests-2.19.1urllib3-1.23

For deactivating the virtual environment, we can use the following command −

(websc) (base) D:\ProgramData\webscrap>deactivate(base) D:\ProgramData\webscrap>

You can see that (websc) has been deactivated.

Python Modules for Web Scraping

Web scraping is the process of constructing an agent which can extract, parse, download and organize useful information from the web automatically. In other words, instead of manually saving the data from websites, the web scraping software will automatically load and extract data from multiple websites as per our requirement.

In this section, we are going to discuss about useful Python libraries for web scraping.

Requests

It is a simple python web scraping library. It is an efficient HTTP library used for accessing web pages. With the help ofRequests, we can get the raw HTML of web pages which can then be parsed for retrieving the data. Before usingrequests, let us understand its installation.

Installing Requests

We can install it in either on our virtual environment or on the global installation. With the help ofpip command, we can easily install it as follows −

(base) D:\ProgramData> pip install requestsCollecting requestsUsing cachedhttps://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whlRequirement already satisfied: idna<2.8,>=2.5 in d:\programdata\lib\sitepackages(from requests) (2.6)Requirement already satisfied: urllib3<1.24,>=1.21.1 ind:\programdata\lib\site-packages (from requests) (1.22)Requirement already satisfied: certifi>=2017.4.17 in d:\programdata\lib\sitepackages(from requests) (2018.1.18)Requirement already satisfied: chardet<3.1.0,>=3.0.2 ind:\programdata\lib\site-packages (from requests) (3.0.4)Installing collected packages: requestsSuccessfully installed requests-2.19.1

Example

In this example, we are making a GET HTTP request for a web page. For this we need to first import requests library as follows −

In [1]: import requests

In this following line of code, we use requests to make a GET HTTP requests for the url:https://authoraditiagarwal.com/ by making a GET request.

In [2]: r = requests.get('https://authoraditiagarwal.com/')

Now we can retrieve the content by using.text property as follows −

In [5]: r.text[:200]

Observe that in the following output, we got the first 200 characters.

Out[5]: '<!DOCTYPE html>\n<html lang="en-US"\n\titemscope\n\titemtype="http://schema.org/WebSite" \n\tprefix="og: http://ogp.me/ns#">\n<head>\n\t<meta charset="UTF-8" />\n\t<meta http-equiv="X-UA-Compatible" content="IE'

Urllib3

It is another Python library that can be used for retrieving data from URLs similar to therequests library. You can read more on this at its technical documentation athttps://urllib3.readthedocs.io/en/latest/.

Installing Urllib3

Using thepip command, we can installurllib3 either in our virtual environment or in global installation.

(base) D:\ProgramData>pip install urllib3Collecting urllib3Using cachedhttps://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whlInstalling collected packages: urllib3Successfully installed urllib3-1.23

Example: Scraping using Urllib3 and BeautifulSoup

In the following example, we are scraping the web page by usingUrllib3 andBeautifulSoup. We are usingUrllib3 at the place of requests library for getting the raw data (HTML) from web page. Then we are usingBeautifulSoup for parsing that HTML data.

import urllib3from bs4 import BeautifulSouphttp = urllib3.PoolManager()r = http.request('GET', 'https://authoraditiagarwal.com')soup = BeautifulSoup(r.data, 'lxml')print (soup.title)print (soup.title.text)

This is the output you will observe when you run this code −

<title>Learn and Grow with Aditi Agarwal</title>Learn and Grow with Aditi Agarwal

Selenium

It is an open source automated testing suite for web applications across different browsers and platforms. It is not a single tool but a suite of software. We have selenium bindings for Python, Java, C#, Ruby and JavaScript. Here we are going to perform web scraping by using selenium and its Python bindings. You can learn more about Selenium with Java on the linkSelenium.

Selenium Python bindings provide a convenient API to access Selenium WebDrivers like Firefox, IE, Chrome, Remote etc. The current supported Python versions are 2.7, 3.5 and above.

Installing Selenium

Using thepip command, we can installurllib3 either in our virtual environment or in global installation.

pip install selenium

As selenium requires a driver to interface with the chosen browser, we need to download it. The following table shows different browsers and their links for downloading the same.

Chrome

https://www.google.com/intl/en_in/chrome/

Edge

https://developer.microsoft.com/

Firefox

https://github.com/

Safari

https://webkit.org/

Example

This example shows web scraping using selenium. It can also be used for testing which is called selenium testing.

After downloading the particular driver for the specified version of browser, we need to do programming in Python.

First, need to importwebdriver from selenium as follows −

from selenium import webdriver

Now, provide the path of web driver which we have downloaded as per our requirement −

path = r'C:\\Users\\gaurav\\Desktop\\Chromedriver'browser = webdriver.Chrome(executable_path = path)

Now, provide the url which we want to open in that web browser now controlled by our Python script.

browser.get('https://authoraditiagarwal.com/leadershipmanagement')

We can also scrape a particular element by providing the xpath as provided in lxml.

browser.find_element_by_xpath('/html/body').click()

You can check the browser, controlled by Python script, for output.

Scrapy

Scrapy is a fast, open-source web crawling framework written in Python, used to extract the data from the web page with the help of selectors based on XPath. Scrapy was first released on June 26, 2008 licensed under BSD, with a milestone 1.0 releasing in June 2015. It provides us all the tools we need to extract, process and structure the data from websites.

Installing Scrapy

Using thepip command, we can installurllib3 either in our virtual environment or in global installation.

pip install scrapy

For more detail study of Scrapy you can go to the linkScrapy

Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp