How to get ip proxy dynamically
We often use IP proxies to hide the real IP address when doing web crawling, data collection or for privacy protection. Dynamically obtaining IP proxies allows us to better cope with the needs of IP blocking or frequent IP changes. So, let's learn how to get an IP proxy dynamically.
In Python, we can use the requests library and the beautifulsoup library for dynamically obtaining IP proxies. Here is a simple example:
"`ipipgothon
import requests
from bs4 import BeautifulSoup
def get_proxy().
url = 'https://www.shenlongip.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ 58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
proxy_list = []
for tr in soup.find_all('tr')::
if tr.find_all('td'):.
ip = tr.find_all('td')[1].text
port = tr.find_all('td')[2].text
protocol = tr.find_all('td')[5].text
proxy_list.append(f'{protocol}://{ip}:{port}')
return proxy_list
“`
The above example code makes a request to the ipipgo proxy website to get the proxy IP address and put it into the list. In practice, we can also add a timed task to get the proxy IP at regular intervals to realize the function of getting IP proxy dynamically.
How to set up ip proxy
After getting the IP proxy, we need to set it up for crawlers or other web requests. In Python, IP proxies can be set up in the following ways:
"`ipipgothon
import requests
def use_proxy().
proxy_list = get_proxy() # Assuming get_proxy() is the function to get the IP proxy
url = 'http://example.com'
for proxy in proxy_list:
try.
response = requests.get(url, proxies={'http': proxy, 'https': proxy}, timeout=5)
print(f'Accessed successfully using proxy {proxy}: {response.status_code}')
break
except requests.exceptions.RequestException as e:.
print(f'Using proxy {proxy} Access failed: {e}')
“`
In the above example, we first get a list of proxy IPs, then traverse the list and initiate a network request using the requests library. In the request, we set the IP proxy by passing in the proxies parameter. If the request succeeds, we print the access success message and end the loop; otherwise, we catch an exception and continue trying for the next proxy IP.
With the above code example, we can easily set up an IP proxy and dynamically obtain an IP address to better cope with network requests where the IP is blocked or needs to be changed.