Scraping - Change Proxy and Headers for each 403 response

Viewed 15

I am trying to build a program that scraps information from a website and stores it into a csv file. I am facing an issue with my "try" and "except" statement that is supposed to change headers and proxies when I get a 403 response.

To bypass securities I am adding to the url:

  • headers from my headers.yml script (Chrome, Firefox and others made up headers)

  • proxies that I get from https://free-proxy-list.net/, test them and store the good ones in a dictionary (5 of them in my code)

I then loop through these headers and proxies to build the URL and get the information (request).

  • If response 200 : the proxies and headers are supposed to change every 2 pages with the "for" loop and I'm able to retrieve all data.

  • If response 403 : I wanted the program to directly change headers and proxies each time it faced an error (403 code) thanks to a "try" and "except" statement.

However in this case, the program still runs normally when it faces 403 and no change is made to the URL.

Here's a sample of my code, could you please help with this issue?

    

for browser, headers in browser_headers.items():
    print(f"\n\nUsing {browser} headers\n")
    for proxy_url in good_proxies:
        proxies = proxies = {
            "http": proxy_url,
            "https": proxy_url,
        }

        try:
            for i in range(1,3):
                response = requests.get(url + str(i+y), headers=headers, proxies=proxies, timeout=7)
                print(response)
                soup = BeautifulSoup(response.content, 'html.parser')
                Tab = soup.find_all('div', class_= "announceDtl")
                for Zone in Tab:
                    P = Zone.find('span', class_= "announceDtlPrice").text
                    S = Zone.find('span', class_= "announceDtlInfosArea").text
                    Pi = Zone.find('span', class_="announceDtlInfosNbRooms").text
                    A = Zone.find('div', class_= "announcePropertyLocation").text
                    info = [P, S, Pi, A]
                    thewriter.writerow(info)
                    Time.sleep(3)
                print('Page Done', y+i)
            y = y + 2

            if y >= int(limit):
                print("== ALL DATA RETRIEVED ===")
                exit()

        except Exception:
            print(f"Proxy {proxy_url} failed, trying another one")


0 Answers
Related