首页 > 编程知识 正文

python ip代理池,selenium python

时间:2023-05-06 10:43:55 阅读:37648 作者:805

http://www.Sina.com/http://www.Sina.com /

一些网站采取了适当的爬虫类对策。 例如,许多网站可能会检测某个时间访问某个IP的次数,如果访问频率太快,看起来不像普通访问者,则可能会禁止访问该IP。 因此,需要设置几个代理IP,并每隔一定时间更改代理IP。 即使IP被禁止,也可以变更IP继续攀登。

一、简介

正向代理:代理客户端检索数据。 正向代理是为了保护客户端不受责任。

反向代理:代理服务器提供数据。 反向代理是为了保护服务器或负责负载平衡。

爬虫中为什么需要使用代理

http://www.goubanjia.com/

西刺代理店

快速代理

代理的分类:

透明:我知道是代理ip,也知道你真正的ip

匿名:我知道是代理ip,不知道你真正的ip

高隐:不知道是代理ip,不知道你真正的ip

免费代理ip提供网站

http:只能请求http开头的url

https:只能请求https开头的url

匿名度:

importrequestsheaders={ ' user-agent ' : ' Mozilla/5.0 (windows nt 6.1; 双赢64; x64 ) appleWebKit/537.36(khtml, like Gecko ) chrome/73.0.3683.103 safari/537.36 ' } URL=' https://www.Baidu.co ood wd=IP ' #不同的代理IP, 代理IP的类型必须与请求url的协议头匹配proxy _ list=[ { ' http ' 3360 ' 112.115.57.2033603128 ' }、{ ' http ' : ' ] 代理IP proxy=random.choice (proxy _ list )随机获取page_text=requesttext (代理IP proxy=proxy ).textwithopen(IP.html

从西刺代理上面爬IP,反复测试是否可用,建立自己的代理IP池,随时更新抓取网站数据

遇到“”问题没人回答吗? 编辑成立了Python学习交流QQ群:寻找有778463939杂志的伙伴,互相帮助,群里有很好的视频学习教程和PDF电子书!' ' ' importrequestsfromlxmlimportetreeimporttimeimportrandomfromfake _ useragentimportuseragentclassgetproxyip (对象) 336666 I.com/nn/' self.proxies={ ' http ' : ' 3358163.204.247.21933609999 } ' https ' : ' http://163.204.22209 user-agent defget _ random _ ua (self ) : ua=创建随机生成用户代理的用户代理对象user agent=ua.randomreturnusers 来自西刺代理网站的随机代理IPdefget_IP_file(url ) : headers={ ' user-agent ' : self.get _ random _ u u ua (} html=requuent ) Proxies=seequests.get timeout=5).content.decode(utf-8 ), ignore ) ) parse_html=etree.html ) tr_list=parse_html与每个代理IP匹配的节点对象列表fortrintr _ list [ 13: ] 333666

)[0] self.test_proxy_ip(ip, port) # 测试ip:port是否可用 # 测试抓取的代理IP是否可用 def test_proxy_ip(self, ip, port): proxies = { 'http': 'http://{}:{}'.format(ip, port), 'https': 'https://{}:{}'.format(ip, port), } test_url = 'http://www.baidu.com/' try: res = requests.get(url=test_url, proxies=proxies, timeout=8) if res.status_code == 200: print(ip, ":", port, 'Success') with open('proxies.txt', 'a') as f: f.write(ip + ':' + port + 'n') except Exception as e: print(ip, port, 'Failed') def main(self): for i in range(1, 1001): url = self.url.format(i) self.get_ip_file(url) time.sleep(random.randint(5, 10))if __name__ == '__main__': spider = GetProxyIP() spider.main()

从IP池中取IP,也就是在爬虫程序中从文件随机获取代理IP

import randomimport requestsclass BaiduSpider(object): def __init__(self): self.url = 'http://www.baidu.com/' self.headers = {'User-Agent': 'Mozilla/5.0'} self.flag = 1 def get_proxies(self): with open('proxies.txt', 'r') as f: result = f.readlines() # 读取所有行并返回列表 proxy_ip = random.choice(result)[:-1] # 获取了所有代理IP L = proxy_ip.split(':') proxy_ip = { 'http': 'http://{}:{}'.format(L[0], L[1]), 'https': 'https://{}:{}'.format(L[0], L[1]) } return proxy_ip def get_html(self): proxies = self.get_proxies() if self.flag <= 3: try: html = requests.get(url=self.url, proxies=proxies, headers=self.headers, timeout=5).text print(html) except Exception as e: print('Retry') self.flag += 1 self.get_html()if __name__ == '__main__': spider = BaiduSpider() spider.get_html()

2.收费代理API

写一个获取收费开放API代理的接口

'''遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!'''import requestsfrom fake_useragent import UserAgentua = UserAgent() # 创建User-Agent对象useragent = ua.randomheaders = {'User-Agent': useragent}def ip_test(ip): url = 'http://www.baidu.com/' ip_port = ip.split(':') proxies = { 'http': 'http://{}:{}'.format(ip_port[0], ip_port[1]), 'https': 'https://{}:{}'.format(ip_port[0], ip_port[1]), } res = requests.get(url=url, headers=headers, proxies=proxies, timeout=5) if res.status_code == 200: return True else: return False# 提取代理IPdef get_ip_list(): # 快代理:https://www.kuaidaili.com/doc/product/dps/ api_url = 'http://dev.kdlapi.com/api/getproxy/?orderid=946562662041898&num=100&protocol=1&method=2&an_an=1&an_ha=1&sep=2' html = requests.get(api_url).content.decode('utf-8', 'ignore') ip_port_list = html.split('n') for ip in ip_port_list: with open('proxy_ip.txt', 'a') as f: if ip_test(ip): f.write(ip + 'n')if __name__ == '__main__': get_ip_list()

3.私密代理

1、语法结构

用户名和密码会在给API_URL的时候给。不是自己的账号和账号密码。

proxies = {'协议':'协议://用户名:密码@IP:端口号'}proxies = { 'http':'http://用户名:密码@IP:端口号', 'https':'https://用户名:密码@IP:端口号'}proxies = { 'http': 'http://309435365:szayclhp@106.75.71.140:16816', 'https':'https://309435365:szayclhp@106.75.71.140:16816',}

获取开放代理的接口

import requestsfrom fake_useragent import UserAgentua = UserAgent() # 创建User-Agent对象useragent = ua.randomheaders = {'User-Agent': useragent}def ip_test(ip): url = 'https://blog.csdn.net/qq_34218078/article/details/90901602/' ip_port = ip.split(':') proxies = { 'http': 'http://1786088386:b95djiha@{}:{}'.format(ip_port[0], ip_port[1]), 'https': 'http://1786088386:b95djiha@{}:{}'.format(ip_port[0], ip_port[1]), } res = requests.get(url=url, headers=headers, proxies=proxies, timeout=5) if res.status_code == 200: print("OK") return True else: print(res.status_code) print("错误") return False# 提取代理IPdef get_ip_list(): # 快代理:https://www.kuaidaili.com/doc/product/dps/ api_url = 'http://dps.kdlapi.com/api/getdps/?orderid=986603271748760&num=1000&signature=z4a5b2rpt062iejd6h7wvox16si0f7ct&pt=1&sep=2' html = requests.get(api_url).content.decode('utf-8', 'ignore') ip_port_list = html.split('n') for ip in ip_port_list: with open('proxy_ip.txt', 'a') as f: if ip_test(ip): f.write(ip + 'n')if __name__ == '__main__': get_ip_list()

思路:

写一个类;get_ip() requests请求接口,得到ip和port;test_ip()请求某一网站,根据状态码或in判断是否有某一内容来判断此ip是否可用,返回Ture和False即可;save_ip()测试成功后保存;

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。