Go Web Scraping Quick Start Guide

Book description

Learn how some Go-specific language features help to simplify building web scrapers along with common pitfalls and best practices regarding web scraping.

Key Features

  • Use Go libraries like Goquery and Colly to scrape the web
  • Common pitfalls and best practices to effectively scrape and crawl
  • Learn how to scrape using the Go concurrency model

Book Description

Web scraping is the process of extracting information from the web using various tools that perform scraping and crawling. Go is emerging as the language of choice for scraping using a variety of libraries. This book will quickly explain to you, how to scrape data data from various websites using Go libraries such as Colly and Goquery.

The book starts with an introduction to the use cases of building a web scraper and the main features of the Go programming language, along with setting up a Go environment. It then moves on to HTTP requests and responses and talks about how Go handles them. You will also learn about a number of basic web scraping etiquettes.

You will be taught how to navigate through a website, using a breadth-first and then a depth-first search, as well as find and follow links. You will get to know about the ways to track history in order to avoid loops and to protect your web scraper using proxies.

Finally the book will cover the Go concurrency model, and how to run scrapers in parallel, along with large-scale distributed web scraping.

What you will learn

  • Implement Cache-Control to avoid unnecessary network calls
  • Coordinate concurrent scrapers
  • Design a custom, larger-scale scraping system
  • Scrape basic HTML pages with Colly and JavaScript pages with chromedp
  • Discover how to search using the "strings" and "regexp" packages
  • Set up a Go development environment
  • Retrieve information from an HTML document
  • Protect your web scraper from being blocked by using proxies
  • Control web browsers to scrape JavaScript sites

Who this book is for

Data scientists, and web developers with a basic knowledge of Golang wanting to collect web data and analyze them for effective reporting and visualization.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Go Web Scraping Quick Start Guide
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  5. 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. Conventions used
    4. Get in touch
      1. Reviews
  6. Introducing Web Scraping and Go
    1. What is web scraping?
    2. Why do you need a web scraper?
      1. Search engines
      2. Price comparison
      3. Building datasets
    3. What is Go?
    4. Why is Go a good fit for web scraping?
      1. Go is fast
      2. Go is safe
      3. Go is simple
    5. How to set up a Go development environment
      1. Go language and tools
      2. Git
      3. Editor
    6. Summary
  7. The Request/Response Cycle
    1. What do HTTP requests look like?
      1. HTTP request methods
      2. HTTP headers
      3. Query parameters
      4. Request body
    2. What do HTTP responses look like?
      1. Status line
      2. Response headers
      3. Response body
    3. What are HTTP status codes?
      1. 100–199 range
      2. 200–299 range
      3. 300–399 range
      4. 400–499 range
      5. 500–599 range
    4. What do HTTP requests/responses look like in Go?
      1. A simple request example
    5. Summary
  8. Web Scraping Etiquette
    1. What is a robots.txt file?
    2. What is a User-Agent string?
      1. Example
    3. How to throttle your scraper
    4. How to use caching
      1. Cache-Control
      2. Expires
      3. Etag
      4. Caching content in Go
    5. Summary
  9. Parsing HTML
    1. What is the HTML format?
      1. Syntax
      2. Structure
    2. Searching using the strings package
      1. Example – Counting links
      2. Example – Doctype check
    3. Searching using the regexp package
      1. Example – Finding links
      2. Example – Finding prices
    4. Searching using XPath queries
      1. Example – Daily deals
      2. Example – Collecting products
    5. Searching using Cascading Style Sheets selectors
      1. Example – Daily deals
      2. Example – Collecting products
    6. Summary
  10. Web Scraping Navigation
    1. Following links
      1. Example – Daily deals
    2. Submitting forms
      1. Example – Submitting searches
      2. Example – POST method
    3. Avoiding loops
    4. Breadth-first versus depth-first crawling
      1. Depth-first
      2. Breadth-first
    5. Navigating with JavaScript
      1. Example – Book reviews
    6. Summary
  11. Protecting Your Web Scraper
    1. Virtual private servers
    2. Proxies
      1. Public and shared proxies
      2. Dedicated proxies
        1. Price
        2. Location
        3. Type
        4. Anonymity
      3. Proxies in Go
    3. Virtual private networks
    4. Boundaries
      1. Whitelists
      2. Blacklists
    5. Summary
  12. Scraping with Concurrency
    1. What is concurrency
    2. Concurrency pitfalls
      1. Race conditions
      2. Deadlocks
    3. The Go concurrency model
      1. Goroutines
      2. Channels
    4. sync package helpers
      1. Conditions
      2. Atomic counters
    5. Summary
  13. Scraping at 100x
    1. Components of a web scraping system
      1. Queue
      2. Cache
      3. Storage
      4. Logs
    2. Scraping HTML pages with colly
    3. Scraping JavaScript pages with chrome-protocol
      1. Example – Amazon Daily Deals
    4. Distributed scraping with dataflowkit
      1. The Fetch service
      2. The Parse service
    5. Summary
  14. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Go Web Scraping Quick Start Guide
  • Author(s): Vincent Smith
  • Release date: January 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789615708