Hands-On Web Scraping with Python

Book description

Collect and scrape different complexities of data from the modern Web using the latest tools, best practices, and techniques

Key Features

  • Learn different scraping techniques using a range of Python libraries such as Scrapy and Beautiful Soup
  • Build scrapers and crawlers to extract relevant information from the web
  • Automate web scraping operations to bridge the accuracy gap and manage complex business needs

Book Description

Web scraping is an essential technique used in many organizations to gather valuable data from web pages. This book will enable you to delve into web scraping techniques and methodologies.

The book will introduce you to the fundamental concepts of web scraping techniques and how they can be applied to multiple sets of web pages. You'll use powerful libraries from the Python ecosystem such as Scrapy, lxml, pyquery, and bs4 to carry out web scraping operations. You will then get up to speed with simple to intermediate scraping operations such as identifying information from web pages and using patterns or attributes to retrieve information. This book adopts a practical approach to web scraping concepts and tools, guiding you through a series of use cases and showing you how to use the best tools and techniques to efficiently scrape web pages. You'll even cover the use of other popular web scraping tools, such as Selenium, Regex, and web-based APIs.

By the end of this book, you will have learned how to efficiently scrape the web using different techniques with Python and other popular tools.

What you will learn

  • Analyze data and Information from web pages
  • Learn how to use browser-based developer tools from the scraping perspective
  • Use XPath and CSS selectors to identify and explore markup elements
  • Learn to handle and manage cookies
  • Explore advanced concepts in handling HTML forms and processing logins
  • Optimize web securities, data storage, and API use to scrape data
  • Use Regex with Python to extract data
  • Deal with complex web entities by using Selenium to find and extract data

Who this book is for

This book is for Python programmers, data analysts, web scraping newbies, and anyone who wants to learn how to perform web scraping from scratch. If you want to begin your journey in applying web scraping techniques to a range of web pages, then this book is what you need! A working knowledge of the Python programming language is expected.

Downloading the example code for this ebook: You can download the example code files for this ebook on GitHub at the following link: https://github.com/PacktPublishing/Hands-On-Web-Scraping-with-Python. If you require support please email: customercare@packt.com

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Web Scraping with Python
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  7. Section 1: Introduction to Web Scraping
  8. Web Scraping Fundamentals
    1. Introduction to web scraping
    2. Understanding web development and technologies
      1. HTTP
      2. HTML 
        1. HTML elements and attributes
        2. Global attributes
      3. XML
      4. JavaScript
      5. JSON
      6. CSS
      7. AngularJS
    3. Data finding techniques for the web
      1. HTML page source
        1. Case 1
        2. Case 2
      2. Developer tools
      3. Sitemaps
      4. The robots.txt file
    4. Summary
    5. Further reading
  9. Section 2: Beginning Web Scraping
  10. Python and the Web – Using urllib and Requests
    1. Technical requirements
    2. Accessing the web with Python
      1. Setting things up
      2. Loading URLs
    3. URL handling and operations with urllib and requests
      1. urllib
      2. requests
    4. Implementing HTTP methods
      1. GET
      2. POST
    5. Summary
    6. Further reading
  11. Using LXML, XPath, and CSS Selectors
    1. Technical requirements
    2. Introduction to XPath and CSS selector
      1. XPath
      2. CSS selectors
        1. Element selectors
        2. ID and class selectors
        3. Attribute selectors
        4. Pseudo selectors
    3. Using web browser developer tools for accessing web content
      1. HTML elements and DOM navigation
      2. XPath and CSS selectors using DevTools
    4. Scraping using lxml, a Python library
      1. lxml by examples
        1. Example 1 – reading XML from file and traversing through its elements
        2. Example 2 – reading HTML documents using lxml.html
        3. Example 3 – reading and parsing HTML for retrieving HTML form type element attributes
      2. Web scraping using lxml
        1. Example 1 – extracting selected data from a single page using lxml.html.xpath
        2. Example 2 – looping with XPath and scraping data from multiple pages
        3. Example 3 – using lxml.cssselect to scrape content from a page
    5. Summary
    6. Further reading
  12. Scraping Using pyquery – a Python Library
    1. Technical requirements
    2. Introduction to pyquery
    3. Exploring pyquery
      1. Loading documents
      2. Element traversing, attributes, and pseudo-classes
      3. Iterating
    4. Web scraping using pyquery
      1. Example 1 – scraping data science announcements
      2. Example 2 – scraping information from nested links
      3. Example 3 – extracting AHL Playoff results
      4. Example 4 – collecting URLs from sitemap.xml
        1. Case 1 – using the HTML parser
        2. Case 2 – using the XML parser
    5. Summary
    6. Further reading
  13. Web Scraping Using Scrapy and Beautiful Soup
    1. Technical requirements
    2. Web scraping using Beautiful Soup
      1. Introduction to Beautiful Soup
      2. Exploring Beautiful Soup
        1. Searching, traversing, and iterating
          1. Using children and parents
          2. Using next and previous
          3. Using CSS Selectors
      3. Example 1 – listing <li> elements with the data-id attribute 
      4. Example 2 – traversing through elements
      5. Example 3 – searching elements based on attribute values
      6. Building a web crawler
    3. Web scraping using Scrapy
      1. Introduction to Scrapy
      2. Setting up a project
        1. Generating a Spider
        2. Creating an item
        3. Extracting data
          1. Using XPath
          2. Using CSS Selectors
          3. Data from multiple pages
        4. Running and exporting
    4. Deploying a web crawler
    5. Summary
    6. Further reading
  14. Section 3: Advanced Concepts
  15. Working with Secure Web
    1. Technical requirements
    2. Introduction to secure web
      1. Form processing
      2. Cookies and sessions
        1. Cookies
        2. Sessions
      3. User authentication
    3. HTML <form> processing
    4. Handling user authentication
    5. Working with cookies and sessions
    6. Summary
    7. Further reading
  16. Data Extraction Using Web-Based APIs
    1. Technical requirements
    2. Introduction to web APIs
      1. REST and SOAP
        1. REST 
        2. SOAP 
      2. Benefits of web APIs
    3. Accessing web API and data formats
      1. Making requests to the web API using a web browser
        1. Case 1 – accessing a simple API (request and response)
        2. Case 2 – demonstrating status codes and informative responses from the API
        3. Case 3 – demonstrating RESTful API cache functionality
    4. Web scraping using APIs
      1. Example 1 – searching and collecting university names and URLs
      2. Example 2 – scraping information from GitHub events
    5. Summary
    6. Further reading
  17. Using Selenium to Scrape the Web
    1. Technical requirements
    2. Introduction to Selenium
      1. Selenium projects
        1. Selenium WebDriver
        2. Selenium RC
        3. Selenium Grid
        4. Selenium IDE
      2. Setting things up
      3. Exploring Selenium
        1. Accessing browser properties
        2. Locating web elements
    3. Using Selenium for web scraping
      1. Example 1 – scraping product information
      2. Example 2 – scraping book information
    4. Summary
    5. Further reading
  18. Using Regex to Extract Data
    1. Technical requirements
    2. Overview of regular expressions
      1. Regular expressions and Python
    3. Using regular expressions to extract data
      1. Example 1 – extracting HTML-based content
      2. Example 2 – extracting dealer locations
      3. Example 3 – extracting XML content
    4. Summary
    5. Further reading
  19. Section 4: Conclusion
  20. Next Steps
    1. Technical requirements
    2. Managing scraped data
      1. Writing to files
    3. Analysis and visualization using pandas and matplotlib
    4. Machine learning 
      1. ML and AI
      2. Python and ML
      3. Types of ML algorithms
        1. Supervised learning
          1. Classification
          2. Regression
        2. Unsupervised learning
          1. Association
          2. Clustering
        3. Reinforcement learning
    5. Data mining 
      1. Tasks of data mining
        1. Predictive
          1. Classification
          2. Regression
          3. Prediction 
        2. Descriptive
          1. Clustering
          2. Summarization
          3. Association rules
    6. What's next?
    7. Summary 
    8. Further reading
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Web Scraping with Python
  • Author(s): Anish Chapagain
  • Release date: July 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789533392