因为在使用爬虫时常常会检查IP地址,所以有必要找到一个获取IP代理的地方。通过一番骚操做,终于构建了本人第一个代理库,代理库的返回值类型均为列表类型。(说明,这些免费代理天天实时更新,通过测试可用率超60%)另外,为保证代理库能长时间稳定运行,本文对requests库的get请求再一次进行了封装。html
1.Python 3.6
2.requests库
3.免费代理网站python
该免费代理网站提供了两个,一个是提供一个免费代理,另外一个是提供一页免费代理(一页最多15个)。web
import requests def GetFreeProxy(): # 获取一个免费代理 url='https://www.freeip.top/api/proxy_ip' ip=list(range(1)) try: res=requests.get(url=url,timeout=20) # 将返回数据进行json解析 result = res.json() ip[0]=result['data']['ip']+':'+result['data']['port'] return ip except Exception: print('获取代理ip失败!正在重试···') # 异常重调 GetFreeProxy() return 0
def GetFreeProxyListAPI(page=1, country='', isp='', order_by='validated_at', order_rule='DESC'): # 获取一个免费代理列表 # 返回值为list # 参数名 数据类型 必传 说明 例子 # page int N 第几页 1 # country string N 所属国 中国,美国 # isp string N ISP 电信,阿里云 # order_by string N 排序字段 speed:响应速度,validated_at:最新校验时间 created_at:存活时间 # order_rule string N 排序方向 DESC:降序 ASC:升序 data = { 'page': str(page), 'country': country, 'isp': isp, 'order_by': order_by, 'order_rule': order_rule } url = 'https://www.freeip.top/api/proxy_ips' + '?' + str(parse.urlencode(data)) ip = list(range(1)) headers = { 'User-Agent': str(choice(user_agent_list)) } session = requests.session() res = GET(session, url=url, headers=headers) # 解析数据 result = res.json() ip = list(range(int(result['data']['to']))) for i in range(int(result['data']['to'])): ip[i] = result['data']['data'][i]['ip'] + ':' + result['data']['data'][i]['port'] return ip
def GetFreeProxy(): # method=2 is pure-HTTP # 返回值为list headers = { 'User-Agent': str(random.choice(user_agent_list)) } session = requests.session() # Choose the proxy web homeurl = 'https://www.freeip.top/' url = 'https://www.freeip.top/?page=' GET(session=session, url=homeurl, headers=headers) # //tr[1]/td[1] res = GET(session=session, url=url, headers=headers) html = etree.HTML(res.text) # 选择IP数据和端口数据 IP_list_1 = html.xpath('//tr[1]/td[1]') IP_list_2 = html.xpath('//tr[1]/td[2]') IP_list = list(range(1)) IP_list[0] = IP_list_1[0].text + ':' + IP_list_2[0].text return IP_list
def GetFreeProxyList(GetType=1, protocol='https'): # 代理可用率超50% 推荐使用 # method=2 is pure-HTTP # 返回值为list headers = { 'User-Agent': str(choice(user_agent_list)), } session = requests.session() # Choose the proxy web if GetType == 1: homeurl = 'https://www.freeip.top/' url = 'https://www.freeip.top/?page=1&protocol=' + protocol GET(session=session, url=homeurl, headers=headers) elif GetType == 2: homeurl = 'https://www.kuaidaili.com/' url = 'https://www.kuaidaili.com/free/inha/' GET(session=session, url=homeurl, headers=headers) else: print('其余方法暂未支持!') return 0 # Get the IP list num = 1 if _exists('IP.txt'): remove('IP.txt') IP_list = [] while True: res = GET(session=session, url=url, headers=headers) html = etree.HTML(res.content.decode('utf-8')) # 选择IP数据和端口数据 IP_list_1 = html.xpath('//tr/td[1]') IP_list_2 = html.xpath('//tr/td[2]') if GetType == 1: url = 'https://www.freeip.top/?page=' + str(num) + '&protocol=' + protocol IP_list.extend( list(map(lambda ip_list_1, ip_list_2: (ip_list_1.text + ':' + ip_list_2.text), IP_list_1, IP_list_2))) num = num + 1 if len(IP_list_1): continue else: break return IP_list
# 因为免费代理网站不稳定,获取大量代理时容易出现503错误,所以须要屡次重传 def GET(session, url, headers, timeout=15, num=0): try: response = session.get(url=url, headers=headers, timeout=timeout,verify=False) if response.status_code == 200: return response else: print('对方服务器错误,正在进行第%i' % (num + 1) + '次重试···') sleep(0.8) response = GET(session=session, url=url, headers=headers, num=num + 1) return response except Exception: print('链接错误,正在进行第%i' % (num + 1) + '次重试···') sleep(0.8) response = GET(session=session, url=url, headers=headers, num=num + 1) return response
获取一个免费代理:
获取一页免费代理:json