instascrape icon indicating copy to clipboard operation
instascrape copied to clipboard

Add proxy options

Open dtrillo opened this issue 4 years ago • 6 comments

I am trying InstaScrape but I need to set a proxy (otherwise I have an error). How do I have to proceed?

dtrillo avatar Feb 01 '21 10:02 dtrillo

Hello! What is the error that requires the use of a proxy?

Unfortunately using proxies isn't a feature that is supported out of the box but you can probably brew up your own solution using requests builtin proxy support. instascrape can scrape data from HTML so you can use requests (with your needed proxies) to request data and then pass the response's HTML into an instascrape scraper to scrape the data.

chris-greening avatar Feb 05 '21 05:02 chris-greening

Yes, the point is to prepare "something" that you can add to request . I did it in other projects but the problem within instascrape is that I don't know how to acccess request to set my proxy credentials.

dtrillo avatar Feb 08 '21 17:02 dtrillo

@chris-greening Got some code working to scrape and validate proxies, but am not really sure how your library works. Willing to collaborate on this?

MattPChoy avatar Feb 21 '21 01:02 MattPChoy

@dtrillo We had to solve this problem as well. What I ended up doing was subclassing the Session object to pass in to Profile.scrape:

class Session(requests.Session):
    """Custom session subclass that allows passing through a timeout to requests.get"""
    def __init__(self, timeout=None, proxies=None):
        super().__init__()
        self.timeout = timeout
        self.proxies = proxies

    def get(self, url, **kwargs):
        kwargs.setdefault('allow_redirects', True)
        return self.request('GET', url, **kwargs, timeout=self.timeout, proxies=self.proxies)

and then using it this way:

with Session(timeout=20, proxies=self.proxies) as s:
    profile.scrape(session=s)

nickhendo avatar Mar 12 '21 00:03 nickhendo

The above is only necessary for us for authenticated proxies if you want to keep trust_env = True for the Session. Otherwise you can just create a session and add proxies to that without the need for the subclassing

nickhendo avatar Mar 12 '21 02:03 nickhendo

@chris-greening . I am trying to use proxies with InstaScrape to avoid blocking. I tried the solution of @nickhendo but can't get it to work. Any ideas?

chaim6888 avatar Jan 05 '22 14:01 chaim6888