Php Site Crawler for Web Scraping and Crawling

Author

Reads 1.2K

Focused view of programming code displayed on a laptop, ideal for tech and coding themes.
Credit: pexels.com, Focused view of programming code displayed on a laptop, ideal for tech and coding themes.

A PHP site crawler is a software tool that can help you scrape and crawl websites by sending HTTP requests and parsing the responses.

PHP is a popular programming language for web scraping due to its simplicity and flexibility.

You can use a PHP site crawler to extract data from websites, such as product information, reviews, and prices.

It's also useful for monitoring website changes, like tracking updates to a website's content or structure.

Related reading: Python Site Crawler

Installation and Setup

To get started with a PHP site crawler, you need to install the necessary package. This can be done via Composer, a popular dependency manager for PHP.

You'll need to start by installing Goutte through Composer, which will allow you to include the library in your project.

The PHP script that performs the web crawling is created by including the Goutte library in your project, which is a crucial step in setting up your crawler.

Installation

To install the package, you can use Composer, which is a reliable and efficient package manager for PHP.

Composer is a popular choice among developers, and it's easy to use.

You can install the package by running a simple command in your terminal or command prompt.

Setup

Computer Program Language Text
Credit: pexels.com, Computer Program Language Text

To set up the PHP crawler script, start by installing Goutte through Composer, which is a package manager for PHP. You can do this by running a single command in your terminal.

Next, include the Goutte library in your project, which will enable you to perform web crawling tasks. This is done by simply including the library in your code.

You'll then need to input specific details into the code, including the base URL to crawl and proxy settings (if you're using a proxy). This will allow the script to access the web pages you want to crawl.

To input your variables, you'll need to specify the base URL to crawl and your proxy settings (if applicable). This information will be used by the script to access the web pages you want to crawl.

Configuration Options

You can control how deep the crawler goes by setting the maximum crawl depth using the setMaximumDepth method. This allows you to limit the number of pages the crawler visits.

Credit: youtube.com, Change Crawler Options in Pope Tech

By default, the crawler will continue until it has crawled every page of the supplied URL. If you want more control over the crawling process, you can use this method to set a specific depth limit.

The first step in setting up the PHP crawler script is to create the PHP code that will perform the web crawling.

Customization

Customization is key to getting the most out of your PHP site crawler. You can customize how links are extracted from a page by passing a custom UrlParser to the crawler.

This allows you to tailor the crawling process to your specific needs. For instance, you can use the built-in SitemapUrlParser to extract and crawl all links from a sitemap, which even supports sitemap index files.

You can also specify a custom User Agent to respect robots.txt rules. This is especially useful if you're crawling a site that requires specific crawl rules for your User Agent.

Credit: youtube.com, Unlock the Power of Link Extraction with Link Grabber - A Chrome Extension for SEO and Research

Custom link extraction is a powerful feature that allows you to fine-tune how links are extracted from a page.

You can customize the link extraction process by passing a custom UrlParser to the crawler, giving you more control over what links are pulled from a page.

The default parser, LinkUrlParser, extracts links from the href attribute of a tags, but you can choose from other built-in parsers, such as SitemapUrlParser, which is specifically designed for sitemap extraction and supports sitemap index files.

If you need to extract links from a sitemap, SitemapUrlParser is the way to go.

Here are some examples of custom selectors you can use to extract links:

Custom Crawl Queue

If you're dealing with a very large site, you may want to store the crawl queue elsewhere, like a database. This is where a custom crawl queue comes in.

You can write your own crawl queue by implementing the Spatie\Crawler\CrawlQueues\CrawlQueue interface. This allows you to pass your custom crawl queue via the setCrawlQueue method on the crawler.

Business professionals using smartphone for data analysis in a modern office setting.
Credit: pexels.com, Business professionals using smartphone for data analysis in a modern office setting.

There are several examples of custom crawl queues you can use, including ArrayCrawlQueue, RedisCrawlQueue, CacheCrawlQueue for Laravel, and Laravel Model as Queue.

Here are some examples of custom crawl queues you can use:

  • ArrayCrawlQueue
  • RedisCrawlQueue (third-party package)
  • CacheCrawlQueue for Laravel (third-party package)
  • Laravel Model as Queue (third-party example app)

In some cases, you may need to store the queue reference after the crawler has finished, using the current crawl limit. This is especially true when crawling across different requests.

Handling and Filtering

The setCrawlProfile-function allows you to tell the crawler which urls to visit or not. You can use the three pre-built CrawlProfiles that come with this package.

These profiles include CrawlAllUrls, CrawlInternalUrls, and CrawlSubdomains. The CrawlAllUrls profile will crawl all urls on all pages, including external sites. The CrawlInternalUrls profile will only crawl internal urls on the pages of a host. The CrawlSubdomains profile will crawl internal urls and its subdomains on the pages of a host.

You can use these profiles to filter and control the crawling process, ensuring you only crawl the urls you need.

Example 3: Combining Total and Crawl Limit

From below road sign regulating vehicle height limits placed above road on city street
Credit: pexels.com, From below road sign regulating vehicle height limits placed above road on city street

Combining total and crawl limits is a powerful way to control your crawler's behavior. By setting both limits, you can ensure that your crawler doesn't overreach its capabilities.

You can set the total crawl limit using the setTotalCrawlLimit method. This limit defines the maximal count of URLs to crawl. For example, you can limit your crawler to crawl a maximum of 100 URLs.

The setCurrentCrawlLimit method, on the other hand, sets a limit on how many URLs will be crawled per execution. This means that even if you have a large total crawl limit, the crawler will still process a limited number of URLs per execution.

Here's a summary of the two limits:

By combining these two limits, you can fine-tune your crawler's behavior to suit your needs. For example, you can set a total crawl limit of 100 URLs and a current crawl limit of 10 URLs per execution. This will ensure that your crawler doesn't overreach its capabilities and can still process a reasonable number of URLs per execution.

Credit: youtube.com, How and When to use Nofollow Links and Follow Links

Accepting nofollow links can be a bit tricky, but it's definitely doable. By default, the crawler will reject all links containing the attribute rel="nofollow".

If you want to disable these checks, you can do so, allowing the crawler to follow these links.

This means you can include nofollow links in your website's navigation or other areas without worrying about the crawler ignoring them.

Just be aware that disabling these checks can potentially allow spammy links to be crawled, so use this feature wisely.

Limiting Content-Type Parsing

You can limit which content-types to parse, which is a great way to save bandwidth and improve crawling efficiency. By setting the setParseableMimeTypes() with an array of allowed types, you can prevent downloading the body of pages that have different mime types, like binary files, audio/video.

This feature can be especially useful when crawling a site, as the crawler will put urls to be crawled in a queue. By default, this queue is stored in memory using the built-in ArrayCrawlQueue.

You can also disable the default behavior of rejecting links containing the attribute rel="nofollow", but this is not directly related to limiting content-type parsing. The key takeaway is that you can control which content-types are parsed by specifying the allowed types.

Execution and Performance

Credit: youtube.com, PHP web crawler running in CLI

In a serverless environment, it's essential to control the crawl behavior to avoid issues. By default, the crawler continues until it has crawled every page it can find.

You can control the crawl behavior with four options: Total Crawl Limit, Current Crawl Limit, Total Execution Time Limit, and Current Execution Time Limit. These limits help you manage the crawler's performance.

Setting a Total Crawl Limit, for example, defines the maximal count of URLs to crawl. You can also set a Current Crawl Limit, which defines how many URLs are processed during the current crawl. The difference between these two limits is crucial to understand.

Concurrent Requests Limit

By default, the package concurrently crawls 10 URLs. You can change this number using the setConcurrency method.

To control the crawl behavior, especially in environments with limitations, you can use the following options: Total Crawl Limit, Current Crawl Limit, Total Execution Time Limit, and Current Execution Time Limit.

Laptop Computer Showing C++ Application
Credit: pexels.com, Laptop Computer Showing C++ Application

The Total Crawl Limit, defined by the setTotalCrawlLimit method, sets the maximum count of URLs to crawl. This can prevent issues in serverless environments.

The Current Crawl Limit, set with setCurrentCrawlLimit, defines how many URLs are processed during the current crawl. This can be useful in environments with resource constraints.

The Total Execution Time Limit, set with setTotalExecutionTimeLimit, sets the maximum execution time of the crawl. This can prevent your crawl from running indefinitely.

You can also set a Current Execution Time Limit, using setCurrentExecutionTimeLimit, to limit the execution time of the current crawl.

Here's a summary of the concurrency limit options:

Crawl and Time Limits

Crawl and Time Limits are crucial factors to consider when it comes to execution and performance. By default, the crawler continues until it has crawled every page it can find, which might cause issues in environments with limitations.

You can control the crawl behavior with four options: Total Crawl Limit, Current Crawl Limit, Total Execution Time Limit, and Current Execution Time Limit. These options can be set using the setTotalCrawlLimit, setCurrentCrawlLimit, setTotalExecutionTimeLimit, and setCurrentExecutionTimeLimit methods.

Credit: youtube.com, SEO Basics - Mastering Crawl Budget

For example, setting a Total Crawl Limit will limit the total number of URLs to crawl, no matter how often you call the crawler. On the other hand, setting a Current Crawl Limit will limit the number of URLs crawled per execution.

Here are the four options in a table for easy reference:

It's also worth noting that you can combine these limits to control the crawler even more precisely. Additionally, if you're working in a serverless environment, you may want to consider setting a Total Crawl Limit to prevent issues.

Frameworks and Tools

Spatie/robots-txt is a useful tool for determining if a page may be crawled from robots.txt, robots meta tags, and robot headers.

Goutte and Spatie/Crawler are two popular PHP crawler frameworks, each with their own strengths. They're suitable for different types of tasks.

For projects requiring asynchronous crawling or dynamic sites, Spatie/Crawler is a better fit. It can execute JavaScript and offers a toolkit for advanced users.

Here are some popular crawlers and their features:

  • Spatie/Crawler: Easy to use, powerful crawler that can execute JavaScript.
  • crawlzone/crawlzone: Fast asynchronous crawling framework.
  • zrashwani/arachnid: SEO-focused crawler for collecting link information.
  • nadar/crawler: Website Crawler Implementation with high extendibility and PDF indexing.

Custom User Agent

Credit: youtube.com, How to change User agent in Microsoft Edge

You can specify a custom User Agent to respect robots.txt rules. This allows you to add specific crawl rule groups for your custom User Agent in robots.txt.

To do this, you can simply identify your custom User Agent, such as 'my-agent'. You can then disallow crawling the entire site for crawlers identified by 'my-agent' in your robots.txt file.

By specifying a custom User Agent, you can tailor your crawl rules to your specific needs and avoid accidentally crawling areas of your site that you don't intend to crawl.

Using Multiple Observers

Using multiple observers is a great feature of the WebCrawlerAPI. You can set multiple observers with setCrawlObservers.

The setCrawlObservers method allows you to pass an array of objects that extend the CrawlObserver abstract class. This means you can have multiple observers working together to crawl web pages.

You can also set multiple observers one by one with addCrawlObserver. This method adds a single observer to the list of observers, but you can call it multiple times to add multiple observers.

Choose a Crawler Framework

Credit: youtube.com, Scrape ANY Website With AI For Free - Best AI Web Scraper

Choosing a crawler framework can be a daunting task, but it's essential to get it right. Goutte and Spatie/Crawler are two popular options for PHP web crawling projects.

Goutte is a solid choice for static websites and simple structures. It's simple to use and moderate in terms of memory efficiency. However, it doesn't support JavaScript execution.

Spatie/Crawler, on the other hand, is better equipped for modern web applications, especially those with dynamic, client-side rendering. It has asynchronous crawling capabilities, complies with robots.txt, and can index PDFs.

Here's a comparison of Goutte and Spatie/Crawler:

Ultimately, the choice between Goutte and Spatie/Crawler depends on your project's specific needs. If you're working with static websites, Goutte might be the better fit. However, if you need to handle dynamic websites or execute JavaScript, Spatie/Crawler is the way to go.

Why Choose WebCrawlerAPI?

Choosing the right tool for web crawling can be a daunting task, but WebCrawlerAPI makes it a breeze. With its simple integration, you can get started with just a few lines of code.

Credit: youtube.com, Web Scraping vs Web Crawling Explained

The cloud scalability of WebCrawlerAPI is another major advantage. It can handle large-scale crawling without requiring manual effort, making it perfect for developers who need to process vast amounts of data.

One of the standout features of WebCrawlerAPI is its anti-bot capabilities, which include tools like CAPTCHA bypassing. This means you can scrape websites without getting caught by their security measures.

WebCrawlerAPI also supports multiple output formats, including Markdown, Text, and raw HTML. This flexibility makes it easy to work with different data formats and applications.

The pay-as-you-go pricing model of WebCrawlerAPI is also a huge plus. You only pay for the resources you use, which means you can scale up or down as needed without breaking the bank.

Here are the key benefits of choosing WebCrawlerAPI:

  • Simple Integration: Get started with just a few lines of code.
  • Cloud Scalability: Handles large-scale crawling without manual effort.
  • Anti-Bot Features: Includes tools like CAPTCHA bypassing.
  • Multiple Output Formats: Supports Markdown, Text, and raw HTML.
  • Pay-As-You-Go Pricing: You only pay for the resources you use.

Languages

Languages play a crucial role in software development, and several popular languages are used in frameworks and tools.

Python is a versatile language used in many frameworks, including Django and Flask, which are often used for web development.

For more insights, see: Php Frameworks

Credit: youtube.com, Selecting Programming Languages Frameworks and Tools for your work

JavaScript is another widely used language, particularly in front-end development with frameworks like React and Angular.

TypeScript is a superset of JavaScript that adds optional static typing, used in frameworks like Angular and Vue.js.

Java is a popular language for Android app development and is also used in frameworks like Spring and Hibernate.

C# is a modern language developed by Microsoft, used in frameworks like ASP.NET and Unity.

Web Scraping and Crawling

Web scraping and crawling are essential components of building a PHP site crawler. To control the crawl behavior, you can use the Total Crawl Limit, Current Crawl Limit, Total Execution Time Limit, and Current Execution Time Limit options.

These options allow you to limit the number of URLs crawled and the execution time of the crawl. For example, you can set a Total Crawl Limit of 1000 URLs to prevent the crawler from crawling too many pages.

A custom crawl queue can also be implemented to store the queue elsewhere, such as a database. This is useful for large sites where the default queue may not be sufficient.

Here are some examples of custom crawl queues:

  • ArrayCrawlQueue
  • RedisCrawlQueue (third-party package)
  • CacheCrawlQueue for Laravel (third-party package)
  • Laravel Model as Queue (third-party example app)

By implementing a custom crawl queue, you can improve the efficiency and scalability of your PHP site crawler.

Change Default Base URL Scheme

Credit: youtube.com, Scrape Data for AI and LLMs: Website Content Crawler Tutorial

Changing the default base URL scheme is a simple yet important step in web scraping and crawling. You can do this with the setDefaultScheme method.

The default base URL scheme is set to http if none, but you can change it to suit your needs. This is particularly useful when working with websites that use https by default.

You can change the default scheme to https if you're working with a website that uses it exclusively. This ensures that your crawler doesn't get blocked by the website's security settings.

By changing the default scheme, you can avoid potential issues that might arise from using the wrong protocol. It's a small tweak that can make a big difference in the success of your web scraping and crawling project.

Filtering Certain URLs

Filtering Certain URLs can be a challenge when web scraping and crawling.

The setCrawlProfile-function allows you to tell the crawler which urls to visit and which to avoid. This function expects an object that extends Spatie\Crawler\CrawlProfiles\CrawlProfile.

Credit: youtube.com, Web Scraping vs Web Crawling Explained | Differences & Similarities

Spatie provides three CrawlProfiles out of the box: CrawlAllUrls, CrawlInternalUrls, and CrawlSubdomains.

CrawlAllUrls will crawl all urls on all pages, including urls to an external site. This can be useful for scraping data from multiple sources.

CrawlInternalUrls will only crawl the internal urls on the pages of a host. This is a good option if you only want to scrape data from your own website.

CrawlSubdomains will only crawl the internal urls and its subdomains on the pages of a host. This is useful for scraping data from a website with multiple subdomains.

Here's a quick summary of the three CrawlProfiles:

Web Scraping with Guzzle and XPath

Guzzle is a PHP HTTP client that lets you send HTTP requests quickly and easily.

To start web scraping with Guzzle and XPath, you'll need to install Guzzle via composer by executing a command in your terminal.

Once installed, create a new PHP file to add the code, such as guzzle_requests.php.

Credit: youtube.com, XPath Tutorial (and How to Use them for Web Scraping)

For this demonstration, we'll be scraping the Books to Scrape website.

To scrape a website, start by understanding its HTML layout by viewing the page source using your browser's Inspect feature.

Initializing Guzzle, XML, and Xpath involves loading the web page into a string using Guzzle and parsing it using XML.

To target the text content inside a specific tag, use XPath expressions like //ol[@class=”row”] to get the whole list.

Each item in the list has an tag that you can target to extract the book's actual title.

Ignoring robots.txt and robots meta data is possible by disabling the checks in your crawler.

Robots data can come from either a robots.txt file, meta tags, or response headers, and can be parsed using the spatie/robots-txt package.

Readers also liked: Php Redirect to Another Page

Executing JavaScript

Executing JavaScript can be a bit tricky when it comes to web scraping and crawling. By default, the crawler won't execute JavaScript, which means it won't be able to get the body HTML after JavaScript has been executed.

Credit: youtube.com, How I Scrape JAVASCRIPT websites with Python

To enable JavaScript execution, you'll need to use our Browsershot package, which depends on Puppeteer under the hood. You can install it by following the pointers on how to install it on your system.

Browsershot will make an educated guess as to where its dependencies are installed on your system, so you don't need to worry about that. However, if you need to set a custom created instance of Browsershot, you can use the setBrowsershot method.

Note that the crawler will still work even if you don't have the system dependencies required by Browsershot, but you won't be able to call executeJavaScript().

Setting Crawl Depth

Setting Crawl Depth is a crucial step in web scraping and crawling. You can limit the depth of the crawler by using the setMaximumDepth method. This method allows you to control how far the crawler goes in crawling the web.

By default, the crawler continues until it has crawled every page of the supplied URL. You can adjust this behavior to suit your needs.

The setMaximumDepth method is a key feature of web crawling scripts. It helps prevent overwhelming your system with too much data.

Crawlers

Credit: youtube.com, Web Scraping vs Web Crawling Explained | Differences & Similarities

Crawlers are an essential part of web scraping and crawling, allowing you to efficiently navigate and extract data from websites. Spatie/Crawler is a powerful and easy-to-use crawler implemented in PHP, capable of executing JavaScript and offering a toolkit for those who want to harness its full potential.

For those looking for alternatives, crawlzone/crawlzone is a fast asynchronous crawling framework, while zrashwani/arachnid is an SEO-focused crawler for collecting link information and more. If you're looking for a high extendible and memory-efficient crawler, nadar/crawler is a great option, written in PHP and capable of indexing PDFs.

If you're new to web scraping and crawling, Spatie/Crawler is a great place to start due to its ease of use and powerful features.

Patricia Dach

Junior Copy Editor

Patricia Dach is a meticulous and detail-oriented Copy Editor with a passion for refining written content. With a keen eye for grammar and syntax, she ensures that articles are polished and error-free. Her expertise spans a range of topics, from technology to lifestyle, and she is well-versed in various style guides.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.