Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added proxy support for Python3 #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brandomr
Copy link

What's New

This PR implements proxy support for Python3 usage of the library. Now you can very easily use a proxy by passing a proxy url to the proxy argument for the search function:

from googlesearch import search
for url in search('"Breaking Code" WordPress blog', stop=20, proxy=http:proxy.ip.address:proxy_port):
    print(url)

@MarioVilas
Copy link
Owner

Why just Py3? The urllib module in Py2 also supports proxies, and it already does by setting the corresponding environment variable. I'd rather not clutter the API with even more parameters, TBH.

@brandomr
Copy link
Author

That was just laziness I guess. And to your point, I think my use case is somewhat an edge case: only proxy requests to Google, but do not proxy requests to other domains. I'm fine closing this and just using my implementation when I need it if that's your preference.

@MarioVilas
Copy link
Owner

Mhm, that's an interesting use case. May I ask a bit more? :)

@brandomr
Copy link
Author

It's just for situations where for speed reasons I don't want to use different proxies for different tasks.

@LeMoussel
Copy link

Very interesting. When will it be committed?

@YLaido
Copy link

YLaido commented Dec 27, 2020

Adding proxy support is definitely a great enhancement to this project. As a Windows user I don't think I can just set a proxy as easily as on Linux. Can't see why it hasn't been merged so far.

@MarioVilas
Copy link
Owner

Setting the environment variable works the same in Windows as in Linux. I haven't merged it because I want to keep all functionality working in all versions of Python.

@santana-guilherme
Copy link

santana-guilherme commented Apr 7, 2021

Setting the environment variable also works, but if you need to change(the proxy) in the middle of the program the urlopen doesn't refresh the environment variable value.

So if you do:

import os
from urllib.request import urlopen

#List of proxies
proxies = [
        '191.96.71.118:3128',
        '186.219.96.47:54570',
    ]
for p in proxies:
    os.environ['http_proxy'] = 'http://' + p
    url = 'http://ipecho.net/plain'
    print(f'Trying {p}')
    res = urlopen(url)
    print('Used ', res)

The result is:

Trying 191.96.71.118:3128
Used 191.96.71.118
Trying 186.219.96.47:54570
Used 191.96.71.118

So i think is a valid feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants