pythonpython-3.xselenium-webdriverselenium-chromedriverundetected-chromedriver

How can I bypass Facebook's IP blocking when scraping ads library with Selenium and undetected-selenium?


I'm currently working on a project where I need to scrape data from Facebook's ads library and upload it to an AWS server. I've set up a web scraper using Selenium and undetected-selenium, but I'm running into an issue where Facebook temporarily blocks my IP address with the message:

" You’re Temporarily Blocked It looks like you were misusing this feature by going too fast. You’ve been temporarily blocked from using it. "

I've noticed that even if I use a private Chrome window with VPN enabled or pass a proxy to the Chrome driver, I still get this block message. The block is lifted when I switch back to my residential IP address.

Is there any way to mask or fake my IP address when interacting with Facebook using Selenium or undetected-selenium? I've attached my code below for reference:

import chromedriver_autoinstaller
import undetected_chromedriver as uc
import time

options = uc.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument('--allow-running-insecure-content')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-application-cache')
options.add_argument("--disable-setuid-sandbox")
options.add_argument("--window-size=1920,1080")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-infobars")
options.add_argument("--mute-audio")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
PROXY = "IP:PORT"
options.add_argument("--proxy-server=%s" % PROXY)
version_main = int(chromedriver_autoinstaller.get_chrome_version().split(".")[0])
driver = uc.Chrome(options=options, version_main=version_main)
driver.get("https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=ALL&q=restaurants&sort_data["
           "direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_unordered&media_type=all")

# Wait to see the error
time.sleep(10)
driver.quit()

Summary: I just want to deploy this script on AWS with AWS default ip and it is not working there.

Any suggestions or solutions to help me bypass this IP blocking issue would be greatly appreciated. Thanks in advance!


Solution

  • Solution:

    I buy residential IPs which does the work, but they are way too expensive.