Home

resources

web scraping faq

python

requests issuer certificate

How to fix local issuer certificate in Python issues?

Encountering a 'local issuer certificate' error in Python can halt your data extraction efforts. This brief guide provides essential steps to resolve this common issue, ensuring your scripts run smoothly.

Best practices

  • Ensure your Python environment is configured with the latest version of the `certifi` package to manage certificates more reliably.

  • Always provide the correct path to your CA bundle file when using the `verify` parameter in `requests.get()` to avoid SSL certificate errors.

  • Avoid disabling SSL certificate verification in production environments to maintain security and data integrity.

  • Regularly update your local certificate store or CA bundle to keep up with new and revoked certificates, enhancing security.

# Import required modules
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.packages.urllib3 import disable_warnings

# Example URL
url = "https://sandbox.oxylabs.io/products"

# Method 1: Disable SSL Verification
response = requests.get(url, verify=False)
print(response.text) # Print the response text

# Method 2: Disable warnings when not verifying SSL
disable_warnings(InsecureRequestWarning)
response = requests.get(url, verify=False)
print(response.text) # Print the response text

# Method 3: Provide a path to a CA_BUNDLE file or directory with certificates
response = requests.get(url, verify='/path/to/certfile')
print(response.text) # Print the response text

# Method 4: Use certifi package to provide CA bundle
import certifi
response = requests.get(url, verify=certifi.where())
print(response.text) # Print the response text

Common issues

  • Ensure the `requests` library is up-to-date to avoid known bugs with SSL certificate handling.

  • Use environment variables to manage paths to your CA certificates, making your code more portable and secure.

  • If using a custom CA bundle, verify its contents regularly to ensure it includes all necessary root and intermediate certificates.

  • Consult the documentation for your specific Python version and libraries to understand any peculiarities in SSL/TLS implementation.

# Incorrect: Hardcoding the path to CA certificates
response = requests.get(url, verify='/absolute/path/to/certfile')

# Correct: Using environment variables for CA certificate paths
import os
response = requests.get(url, verify=os.getenv('CA_CERT_PATH'))

# Incorrect: Using an outdated requests library that might not handle SSL properly
response = requests.get(url, verify=True)

# Correct: Ensuring the requests library is updated
# pip install --upgrade requests
response = requests.get(url, verify=True)

# Incorrect: Not checking the contents of a custom CA bundle
response = requests.get(url, verify='/path/to/custom/cabundle.pem')

# Correct: Regularly updating and verifying the custom CA bundle
# Ensure the CA bundle file includes necessary certificates
response = requests.get(url, verify='/path/to/updated/custom/cabundle.pem')

# Incorrect: Ignoring SSL/TLS implementation details in Python documentation
response = requests.get(url, verify=True)

# Correct: Consulting Python and library documentation for SSL/TLS peculiarities
# Check Python and requests documentation regarding SSL/TLS best practices
response = requests.get(url, verify=True)

Try Oyxlabs' Proxies & Scraper API

Residential Proxies

Self-Service

Human-like scraping without IP blocking

From

8

Datacenter Proxies

Self-Service

Fast and reliable proxies for cost-efficient scraping

From

1.2

Web scraper API

Self-Service

Public data delivery from a majority of websites

From

49

Useful resources

Get the latest news from data gathering world

I'm interested