Smart Proxy library to get random proxy using Python [Hide your Identity]¶
from IPython.display import YouTubeVideo
YouTubeVideo('_z4DhRUg2Vs')
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 *
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¶
# 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)
Here you see it tried Different Proxy which ever was working we used that IP and got Response 200¶
The Library¶
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
Really helped me Bro Nice Work . i was planning to do similar , But you had already made Keep it up
ReplyDeletePythonist: Smart Proxy Library To Get Random Proxy Using Python [Hide Your Identity] >>>>> Download Now
Delete>>>>> 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
can i use this in mechanize module.
ReplyDeleteTook 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
ReplyDeleteThis 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
ReplyDeletehow 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) ?
ReplyDeleteHi, 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
ReplyDeleteHi, 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?
ReplyDeletehai, it's very helpful...
ReplyDeleteThank you very much...
sy4m
Wonderful article, Thank you for sharing amazing blog write-ups.
ReplyDeleteYou can also check out another blog on Cryptography and Network Security
I don't understand why you defined `break` just after `return r` within `Proxy_Request()` function. In no circumstances will `break` be called.
ReplyDeleteHow do we read the content of a website further?
ReplyDeleteI am getting this error.
ReplyDeleteConnectionError: ('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)
There is an internet issue with you i hope fixed that but the answer is for others
DeletePretty 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
ReplyDeleteThis 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