Tuesday, May 7, 2019

Smart Proxy library to get random proxy using Python [Hide your Identity]

Smart Library that's fetch Random Proxy using Python

Smart Proxy library to get random proxy using Python [Hide your Identity]

In [3]:
from IPython.display import YouTubeVideo
YouTubeVideo('_z4DhRUg2Vs')
Out[3]:

Article By

Soumil Nitin Shah

Bachelor in Electronic Engineering Master in Electrical Engineering Master in Computer Engineering

Graduate Teaching/Research Assistant

Python Developer

soushah@my.bridgeport.edu

————————————————————————————————————

Blog http://soumilshah1995.blogspot.com

Linkedin: https://www.linkedin.com/in/shah-soumil

Github https://github.com/soumilshah1995

Youtube channel https://www.youtube.com/channel/UC_eOodxvwS_H7x2uLQa-svw


Objective:

Whenever you are doing webscrapping on any website its important that you hide your identity. other wise server might block you. Getting Proxy can be tough and also sometime costly. in this i have made a smart library with just 4 lines of code you can get any random IP Address and port number so you can use them when you are doing webscrapping

important Note:

Assume your name of your python file is main and the code that i am about to show you is the library developed to do job in 3 lines of code. Paster the Entire python code in your File called main and all way down start writing your code

copy the library ie python file in your working Directory and you can simple say from nameofpythonfile import *

Assume your Work Directory is

Foldername ---- IoTclass

                    > main.py

                    > nameoffile.py

in your main Python File you will say from nameoffile import *

if that dosent work for some reason you can also copy entire 500 Line of library code in main.py and all the way down start writing your code

How to use

In [2]:
# Create a class
proxy = Random_Proxy()

url = 'https://www.youtube.com'
request_type = "get"

r = proxy.Proxy_Request(url=url, request_type=request_type)
print(r)
Using Proxy {'https': '109.70.189.70:30480'}
Using Proxy {'https': '109.101.139.126:59254'}
Using Proxy {'https': '45.71.80.34:46505'}
Using Proxy {'https': '186.42.175.138:47601'}
Using Proxy {'https': '31.42.173.57:34639'}
Using Proxy {'https': '37.59.35.174:8080'}
Using Proxy {'https': '202.166.211.182:61767'}
<Response [200]>

Here you see it tried Different Proxy which ever was working we used that IP and got Response 200

The Library

In [1]:
try:

    import requests
    from bs4 import BeautifulSoup
    import random

except:
    print(" Library Not Found !")


class Random_Proxy(object):

    def __init__(self):
        self.__url = 'https://www.sslproxies.org/'
        self.__headers = {
            'Accept-Encoding': 'gzip, deflate, sdch',
            'Accept-Language': 'en-US,en;q=0.8',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Referer': 'http://www.wikipedia.org/',
            'Connection': 'keep-alive',
            }
        self.random_ip = []
        self.random_port = []

    def __random_proxy(self):

        """
        This is Private Function Client Should not have accesss
        :return: Dictionary object of Random proxy and port number
        """

        r = requests.get(url=self.__url, headers=self.__headers)
        soup = BeautifulSoup(r.text, 'html.parser')

        # Get the Random IP Address
        for x in soup.findAll('td')[::8]:
            self.random_ip.append(x.get_text())

        # Get Their Port
        for y in soup.findAll('td')[1::8]:
            self.random_port.append(y.get_text())

        # Zip together
        z = list(zip(self.random_ip, self.random_port))

        # This will Fetch Random IP Address and corresponding PORT Number
        number = random.randint(0, len(z)-50)
        ip_random = z[number]

        # convert Tuple into String and formart IP and PORT Address
        ip_random_string = "{}:{}".format(ip_random[0],ip_random[1])

        # Create a Proxy
        proxy = {'https':ip_random_string}

        # return Proxy
        return proxy

    def Proxy_Request(self,request_type='get',url='',**kwargs):
        """

        :param request_type: GET, POST, PUT
        :param url: URL from which you want to do webscrapping
        :param kwargs: any other parameter you pass
        :return: Return Response
        """
        while True:
            try:
                proxy = self.__random_proxy()
                print("Using Proxy {}".format(proxy))
                r = requests.request(request_type,url,proxies=proxy,headers=self.__headers ,timeout=8, **kwargs)
                return r
                break
            except:
                pass

16 comments:

  1. Really helped me Bro Nice Work . i was planning to do similar , But you had already made Keep it up

    ReplyDelete
    Replies
    1. Pythonist: Smart Proxy Library To Get Random Proxy Using Python [Hide Your Identity] >>>>> Download Now

      >>>>> Download Full

      Pythonist: Smart Proxy Library To Get Random Proxy Using Python [Hide Your Identity] >>>>> Download LINK

      >>>>> Download Now

      Pythonist: Smart Proxy Library To Get Random Proxy Using Python [Hide Your Identity] >>>>> Download Full

      >>>>> Download LINK 15

      Delete
  2. can i use this in mechanize module.

    ReplyDelete
  3. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! The Random Blogger

    ReplyDelete
  4. This is a very interesting web page and I have enjoyed reading many of the articles and posts contained on the website, keep up the good work and hope to read some more interesting content in the future.torrentz2 proxy

    ReplyDelete
  5. how to use not the Random but periodically using list.txt? and also using random delay time or periodically delay time (3 sec or 4 mnt visit the website) ?

    ReplyDelete
  6. Hi, could you please help me to add this proxy for the mentioned code. I tried, but unable to do that. Please find the code link: https://stackoverflow.com/questions/63846471/web-scraping-for-google-image-download-for-a-number-of-minimum-2k-images-in-pyth?noredirect=1#comment112901710_63846471

    ReplyDelete
  7. Hi, can you let me know what's this url for? Also, how can we put a limit on it, like if I want to generate 100 requests how should I proceed with that?

    ReplyDelete
  8. hai, it's very helpful...

    Thank you very much...

    sy4m

    ReplyDelete
  9. Wonderful article, Thank you for sharing amazing blog write-ups.

    You can also check out another blog on Cryptography and Network Security

    ReplyDelete
  10. I don't understand why you defined `break` just after `return r` within `Proxy_Request()` function. In no circumstances will `break` be called.

    ReplyDelete
  11. How do we read the content of a website further?

    ReplyDelete
  12. I am getting this error.
    ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

    Here is my code

    proxy = Random_Proxy()
    url = 'https://www.tencent.com/'
    request_type = "get"

    r = proxy.Proxy_Request(url=url, request_type=request_type)
    print(r)

    t = requests.get(url).text()
    print (t)

    ReplyDelete
    Replies
    1. There is an internet issue with you i hope fixed that but the answer is for others

      Delete
  13. Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! digitogy

    ReplyDelete
  14. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! proxies cheap

    ReplyDelete

Record Level Indexing in Apache Hudi Delivers 70% Faster Point Lookups

Untitled2 Record Level Indexing in Apache Hudi Delivers 70% Faster Point Lookups ¶ ...