'Unable to use scraperapi with selenium
I am trying to use the scraperapi with selenium. It runs fine when I use it with python requests with the following code.
import requests
proxies = {
"http": "http://scraperapi:[email protected]:8001",
"https": "http://scraperapi:[email protected]:8001"
}
r = requests.get('http://httpbin.org/ip', proxies=proxies, verify=False)
print(r.text)
It returns the proxy IP with the above code.
But it returns my original IP when I try with following code.
from selenium import webdriver
PATH = 'C:\Program Files (x86)\chromedriver.exe'
proxy = "http://api.scraperapi.com?api_key=my_api_key&render=true"
options = webdriver.ChromeOptions()
options.add_argument(f'--proxy-server={proxy}')
driver = webdriver.Chrome(PATH, options=options)
url = 'http://httpbin.org/ip'
driver.get(url)
Solution 1:[1]
According to ScraperAPI's own guides, the easiest way seems to be using selenium-wire instead of plain selenium
from seleniumwire import webdriver
API_KEY = 'YOUR_API_KEY'
proxy_options = {
'proxy': {
'http': f'http://scraperapi:{API_KEY}@proxy-server.scraperapi.com:8001',
'no_proxy': 'localhost,127.0.0.1'
}
}
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
driver.get("http://httpbin.org/ip")
https://www.scraperapi.com/quick-start-guides/python-selenium-scraper/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chester Cheng |
