python怎么获取代理ip地址

发布时间:2020-05-15编辑:脚本学堂
如何用python获取代理ip地址,用代理上网的环境中,有时会要求获取代理ip地址,可以用python来实现,非常方便。

python代码:
 

复制代码 代码示例:

#!/usr/bin/env python
#coding:utf-8

from bs4 import BeautifulSoup
import requests
import random
 
def getproxyip():
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip,deflate,sdch',
'Host':'www.ip-adress.com',
'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0'
}
 
proxy_url = 'http://www.ip-adress.com/Proxy_Checker/'
proxy_req = requests.get(proxy_url,headers=headers)
proxy_soup = BeautifulSoup(proxy_req.text)
proxy_ip = proxy_soup.find_all('tr',{'class':'yellow'})
return proxy_ip[random.randrange(0,4)].td.text