Here’s a simple Python code snippet that demonstrates how to scrape a website using a proxy:
Make sure to replace ‘proxy_ip’ and ‘proxy_port’ with the appropriate IP address and port number of the proxy you want to use. Additionally, update ‘https://example.com’ with the actual URL you want to scrape.
import requests
# Proxy information
proxy = {
'http': 'http://proxy_ip:proxy_port',
'https': 'http://proxy_ip:proxy_port'
}
# Website URL to scrape
url = 'https://example.com'
try:
# Make a GET request using the proxy
response = requests.get(url, proxies=proxy)
# Check if the request was successful
if response.status_code ==200:
# Process the response content
content = response.content
# Do something with the content...
else:
print(f"Request failed with status code: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
Note that this code uses the requests library, which is commonly used for making HTTP requests in Python. If you haven’t installed it yet, you can install it using pip install requests.