If you‘ve ever tried your hand at web scraping, you‘ve likely run into the frustrating experience of having your script blocked or banned despite your best efforts. Websites are getting increasingly adept at detecting and thwarting web scraping attempts. But by smartly utilizing user agents, you can disguise your scraping script as a regular web browser and avoid detection.
In this in-depth guide, we‘ll cover everything you need to know about user agents for web scraping in 2023. You‘ll learn what user agents are, why they‘re important, how to implement them in your scraping code, and best practices to follow for large-scale scraping projects. Let‘s dive in!
What Are User Agents and Why Do They Matter for Scraping?
In simple terms, a user agent is a string of text that identifies the client application, operating system, software vendor, or software version when making a request to a web server. It essentially tells the server what kind of device/software is trying to access it.
For example, when you visit a website in Chrome, it sends a user agent string to the web server that looks something like this:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
This tells the server that the client is Chrome version 93 running on a 64-bit Windows 10 machine. The server can then optimize the content it returns based on the client‘s capabilities.
So why does this matter for web scraping? Well, most web scraping tools and libraries have their own default user agent strings that immediately give them away. For instance, the popular Python requests library uses "python-requests/2.25.1" as its user agent by default.
Many websites watch for these kinds of "bot-like" user agents and will quickly block any traffic coming from them to prevent scraping. By spoofing your user agent to mimic a regular browser instead, you can slip past these kinds of basic anti-scraping defenses. It‘s the first and most essential step to stealthy scraping.
How to Change User Agents in Your Scraping Code
Fortunately, overriding the default user agent is fairly simple in most web scraping frameworks. In Python with the requests library for example, you can define custom headers that include your own user agent string:
import requests
headers = {
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36‘
}
response = requests.get(‘http://example.com‘, headers=headers)
This will send the specified Chrome 93 user agent in the request headers instead of the default python-requests one. You can use this same approach for other scraping tools by referring to their documentation on setting custom headers.
Which User Agent Strings Should You Use for Scraping?
As a general rule, you‘ll want to use user agent strings of the most common and up-to-date browsers to blend in with regular traffic as much as possible. As of mid-2023, some of the top user agents across desktop and mobile that are recommended for web scraping include:
Desktop:
- Chrome 114/115 on Windows 10/11
- Chrome 114/115 on macOS
- Firefox 115/116 on Windows 10/11
- Microsoft Edge 115 on Windows 10/11
- Safari 16 on macOS
Mobile:
- Chrome 114/115 on Android 12/13
- Safari on iOS 16
- Samsung Browser 20 on Android
- Firefox 115/116 on Android
It‘s best to use a variety or rotation of user agents in combination with proxy rotation (which we‘ll discuss more later) to distribute your scraping requests and avoid establishing a pattern. You can find extensive lists of browser user agent strings on sites like UserAgentString.com that are constantly updated.
User Agents Alone Aren‘t Enough – You Need Proxies Too
While disguising yourself with a legitimate-looking user agent is a good start, it‘s not a fool-proof solution against more sophisticated anti-scraping measures. Many websites monitor for additional red flags like rate of requests, IP address usage, mouse movements, and even graphics rendering.
To really fly under the radar for large-scale scraping jobs, you need to spread out your requests across many different IP addresses using a proxy service. With each request coming from a new IP, the website will have a much harder time correlating the traffic to a single scraping agent.
As the demand for web scraping grows, so too does the market for proxy solutions tailored for scraping. As of 2023, some of the most reputable residential and datacenter proxy providers for web scraping include:
- Bright Data – Industry-leader with a massive residential proxy pool and advanced scraping tools
- IPRoyal – Ethical proxy network with great performance and flexible pricing
- Proxy-Seller – Premium mobile proxy service with worldwide coverage
- SOAX – Reliable proxy platform with residential, mobile, and datacenter IPs
- Smartproxy – High-rotating residential proxies with a user-friendly dashboard
- Proxy-Cheap – Affordable backconnect and dedicated proxies for scraping
- HydraProxy – Fast and stable proxies in many locations worldwide
Be sure to research which proxy type (datacenter, residential, mobile) makes the most sense for your specific scraping targets and scale. Using proxies in tandem with carefully spoofed user agents will give you the best chance of scraping successfully without bans or blocks.
User Agent Best Practices and Precautions for Web Scraping
While there is a lot of flexibility in what you can set your user agent to, there are some general guidelines and warnings to keep in mind:
- Don‘t use made up or overly obscure user agents. Stick to imitating the most widely-used and latest browser versions for the most plausible disguise.
- Rotate through multiple user agents for different requests instead of using the same one repeatedly.
- Always combine user agent spoofing with proxy rotation for the best results, especially for scraping at scale.
- Be aware that circumventing a site‘s countermeasures to scrape may be against their terms of service and could result in an IP ban or even legal action if abused. Respect robots.txt restrictions when possible.
- Limit your request rate and implement random delays/cooldowns to avoid tripping rate limit filters even with a legitimate user agent.
- Keep your user agent strings and proxy IPs updated periodically as new browser versions are released and datacenter proxies get blocked.
Final Thoughts and the Future of Web Scraping
Web scraping is only getting harder as websites deploy more sophisticated defenses to protect their data. But by masquerading as a regular user with natural-looking user agents and distributing your requests across proxy IPs, you can still scrape most public web data you need without being blocked or banned.
Make sure you keep your tools and techniques updated as the web scraping arms race continues to evolve. The list of best user agent strings and top proxy providers in this guide is current for 2023, but change is the only constant in the world of web scraping.
Here‘s to many successful and stealthy scraping projects with the power of user agent spoofing! Let me know in the comments if you have any other user agent tips or run into issues implementing these techniques.
