In modern web scraping, due to the complexity of anti-scraping mechanisms, many developers often encounter obstacles when scraping data. Common anti-scraping techniques include IP restrictions, CAPTCHAs, User-Agent recognition, and JS rendering. To effectively cope with these anti-scraping mechanisms, we need to adopt corresponding strategies.
1. Set a Reasonable User-Agent: Disguise as a Browser
Some websites identify scrapers based on the User-Agent, so we can set a disguised User-Agent to avoid being recognized as a scraper. Here is an example code:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
response = requests.get('https://www.example.com', headers=headers)
print(response.text)
By setting different User-Agents, we can simulate different browsers to bypass the User-Agent recognition anti-scraping mechanism.
2. Use Proxy IPs: Avoid IP Bans
To prevent IP bans, we can use proxy IPs. Here is an example code using a proxy IP:
proxies = {
'http': 'http://10.10.1.10:3128',
'https': 'http://10.10.1.10:1080',
}
response = requests.get('https://www.example.com', proxies=proxies)
print(response.text)
By using proxy IPs, we can hide our real IP address to avoid being banned.
3. Handle CAPTCHAs: Recognize and Bypass CAPTCHAs
Some websites use CAPTCHAs on their pages to prevent scraper access. We can use third-party CAPTCHA recognition services, such as Tesseract or cloud-based CAPTCHA services, to handle CAPTCHAs. Here is an example code using Tesseract to recognize a CAPTCHA:
from PIL import Image
import pytesseract
image = Image.open('captcha.png')
captcha_text = pytesseract.image_to_string(image)
print(captcha_text)
By recognizing CAPTCHAs, we can bypass the CAPTCHA anti-scraping mechanism.
4. Simulate Browser Behavior: Use Selenium or Pyppeteer
For web pages that require JavaScript rendering, we can use Selenium or Pyppeteer to simulate browser behavior. Here is an example code using Pyppeteer:
from pyppeteer import launch
import asyncio
async def main():
browser = await launch()
page = await browser.newPage()
await page.goto('https://www.example.com')
content = await page.content()
print(content)
await browser.close()
asyncio.run(main())
By simulating browser behavior, we can bypass the JS rendering anti-scraping mechanism.
5. Random Access Times: Simulate Human Access Behavior
To simulate human behavior, we can add random wait times when accessing pages. Here is an example code:
import time
import random
for i in range(10):
time.sleep(random.uniform(1, 3))
# Code to send requests and process data
By using random access times, we can simulate human access behavior to bypass anti-scraping mechanisms based on access frequency.
6. Data Storage: Save Scraped Data to Files or Databases
The scraped data can be stored in files or databases. Here is an example of storing data in a CSV file:
import csv
data = [{'Movie Name': 'The Shawshank Redemption', 'Rating': '9.7'}, {'Movie Name': 'Farewell My Concubine', 'Rating': '9.6'}]
with open('movies.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=['Movie Name', 'Rating'])
writer.writeheader()
writer.writerows(data)
By storing data in files or databases, we can easily manage and analyze the scraped data.
The above content combines information and code examples from search results, hoping to help you better understand and cope with anti-scraping challenges.