Automate Web Scraping Using Python‘s AutoScraper Library
Web scraping is an incredibly useful technique that allows you to extract data from websites programmatically. Whether you need to collect product information from e-commerce sites, pull financial data from reports, or gather news from various sources, web scraping empowers you to obtain the data you need efficiently and at scale.
However, web scraping isn‘t always straightforward. Websites are built differently, using various structures, layouts, and technologies. This heterogeneity poses challenges when trying to extract data uniformly. Additionally, websites may explicitly attempt to prevent scraping by detecting and blocking scrapers. Crafting the right selectors to precisely pinpoint the data you want and handling the idiosyncrasies and roadblocks of different sites takes significant work.
Fortunately, there are tools that help alleviate these challenges and make web scraping much easier. One such tool is the AutoScraper library for Python. AutoScraper uses machine learning to automatically learn the scraping rules required to extract the desired data from a page. With just a few lines of code and some sample data, AutoScraper can learn how to fetch the data you specify from the pages you point it to.
Here‘s a step-by-step guide to using AutoScraper to streamline your web scraping:
Installation
First, make sure you have Python and pip installed. Open a terminal and run the following command to install AutoScraper:
pip install autoscraper
You can also install it directly from the Git repository:
pip install git+https://github.com/alirezamika/autoscraper.git
Collecting Sample Data
Next, you need to provide AutoScraper with a URL of a page you want to scrape and some sample data that you want to extract from that page. The sample data serves as clues that AutoScraper uses to deduce the scraping rules.
For example, let‘s say we want to scrape articles about data science from Medium. We‘d collect the URL of Medium‘s data science topic page:
url = ‘https://medium.com/tag/data-science‘
And let‘s say the sample data we want is the titles of a few articles on that page:
wanted_list = [
‘Understanding Random Forests‘,
‘5 Beginner Friendly Steps to Learn Machine Learning and Data Science‘,
‘How to Get Your First Data Science Job‘
]
The sample data can be text content, URLs, or really any data that is identifiable on the page. You can provide multiple samples – the more you give, the better AutoScraper can learn the extraction rules.
Training the Scraper
With the URL and sample data in hand, we‘re ready to instantiate AutoScraper and train it:
from autoscraper import AutoScraper
scraper = AutoScraper()
result = scraper.build(url, wanted_list)
Here we create an AutoScraper instance and call its build method, passing in the URL and sample data. AutoScraper will visit the page, render its content, analyze it to deduce the scraping rules, and extract data that matches the samples. The extracted data is returned in the result.
print(result)
[‘Understanding Random Forests‘,
‘5 Beginner Friendly Steps to Learn Machine Learning and Data Science‘,
‘How to Get Your First Data Science Job‘,
...]
Just like that, AutoScraper learned how to fetch the article titles we specified and extracted all matching results from the page! We can now use the trained scraper to extract data from other pages that have a similar structure:
result2 = scraper.get_result_similar(‘https://medium.com/tag/data-science/latest‘)
print(result2)
[‘Why Automated Feature Engineering Will Change the Way You Do Machine Learning‘,
‘Pruning Decision Trees for Better Explainability‘,
...]
AutoScraper figured out the scraping rules from the initial samples and applied them to extract relevant data from a different page.
Saving and Loading the Trained Scraper
Training the scraper takes some work, so you‘ll often want to save it so you can reuse it later without retraining. AutoScraper makes this easy with its save and load methods:
scraper.save(‘medium_scraper‘)
Now the next time you need to run the scraper, you can load it and start scraping right away:
scraper = AutoScraper()
scraper.load(‘medium_scraper‘)
results = scraper.get_result_similar(‘https://medium.com/tag/machine-learning/archive‘)
Additional Configurations
AutoScraper offers a few additional configurations to fine-tune its behavior:
-
request_args: a dictionary of arguments you want to pass to the underlying requests model, e.g. custom headers, authentication, etc.
-
update: a boolean indicating whether to re-train the scraper even if a trained model already exists. Defaults to True.
-
unique: whether to ensure the extracted results are unique. Defaults to False.
For example, you can configure AutoScraper to use custom request headers:
headers = {
‘User-Agent‘: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36‘
}
scraper = AutoScraper()
result = scraper.build(url, wanted_list, request_args=dict(headers=headers))
Tips and Best Practices
Here are a few tips to get the most out of AutoScraper:
-
Provide a variety of representative samples that cover the main ways the target data appears on the page. The more comprehensive the samples, the better AutoScraper can learn the right rules.
-
If you‘re not getting the results you expect, try providing more specific or unique sample data. This will help AutoScraper zero in on exactly what you want.
-
Be mindful of websites‘ terms of service and robots.txt, which may prohibit scraping. Don‘t abuse sites or scrape data unethically.
-
Websites change over time, so a scraper that works today may break tomorrow. Regularly verify your scraper‘s results and re-train it if needed.
-
AutoScraper works well for many scraping tasks, but it‘s not a silver bullet. For more complex scenarios, you may need to explore other tools like Scrapy or Selenium.
Conclusion
Web scraping is an invaluable tool for extracting data from the vast troves of information on the web. However, it can also be time-consuming and technically challenging. The AutoScraper library offers a compelling solution by automating many of the tricky parts of web scraping through machine learning.
With a simple API and powerful functionality, AutoScraper significantly lowers the barrier to web scraping. By learning scraping rules from the sample data you provide, it takes care of the gnarly details and edge cases involved in extracting data from websites consistently.
Whether you‘re a beginner looking to get started with web scraping or an experienced practitioner searching for ways to streamline your workflow, AutoScraper is a fantastic tool to have in your kit. Give it a try the next time you need to collect data from the web and see how much easier it makes the process!