前言

最近在重新温习python基础-正则,感觉正则很强大,不过有点枯燥,想着,就去应用正则,找点有趣的事玩玩

00xx01---代理IP

有好多免费的ip,不过一个一个保存太难了,也不可能,还是用我们的python爬取吧

00xx02---正则提取ip

  1. import requests
  2. import re
  3. #防反爬
  4. headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
  5. url = "https://www.xicidaili.com/nn/1"
  6. response = requests.get(url,headers=headers)
  7. # print(response.text)
  8. html = response.text
  9. #print(html)
  10. #re.S忽略换行的干扰
  11. ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
  12. ports = re.findall(("<td>(\d+)</td>"),html,re.S)
  13. print(ips)
  14. print(ports)

00xx03---拼接IP和端口

  1. import requests
  2. import re
  3. #防反爬
  4. headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
  5. url = "https://www.xicidaili.com/nn/1"
  6. response = requests.get(url,headers=headers)
  7. # print(response.text)
  8. html = response.text
  9. # print(html)
  10. #re.S忽略换行的干扰
  11. ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
  12. ports = re.findall(("<td>(\d+)</td>"),html,re.S)
  13. #print(ips)
  14. #print(ports)
  15. for ip in zip(ips,ports ): #提取拼接ip和端口
  16. print(ip)

00xx03---验证IP可行性

思路:带着ip和端口去访问一个网站,百度就可以

  1. import requests
  2. import re
  3. headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
  4. for i in range(1,1000):
  5. #网址
  6. url = "https://www.xicidaili.com/nn/{}".format(i)
  7. response = requests.get(url,headers=headers)
  8. # print(response.text)
  9. html = response.text
  10. #re.S忽略换行的干扰
  11. ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
  12. ports = re.findall(("<td>(\d+)</td>"),html,re.S)
  13. # print(ips)
  14. # print(ports)
  15. for ip in zip(ips,ports ): #提取拼接ip和端口
  16. proxies = {
  17. "http":"http://" + ip[0] + ":" + ip[1],
  18. "https":"http://" + ip[0] + ":" + ip[1]
  19. }
  20. try:
  21. res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
  22. print(ip,"能使用")
  23. with open("ip.text",mode="a+") as f:
  24. f.write(":".join(ip)) #写入ip.text文本
  25. f.write("\n") #换行
  26. except Exception as e: #捕捉错误异常
  27. print(ip,"不能使用")

00xx04---写入文本

  1. import requests
  2. import re
  3. #防反爬
  4. headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
  5. url = "https://www.xicidaili.com/nn/1"
  6. response = requests.get(url,headers=headers)
  7. # print(response.text)
  8. html = response.text
  9. # print(html)
  10. #re.S忽略换行的干扰
  11. ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
  12. ports = re.findall(("<td>(\d+)</td>"),html,re.S)
  13. #print(ips)
  14. #print(ports)
  15. for ip in zip(ips,ports ): #提取拼接ip和端口
  16. print(ip)
  17. proxies = {
  18. "http":"http://" + ip[0] + ":" + ip[1],
  19. "https":"http://" + ip[0] + ":" + ip[1]
  20. }
  21. try:
  22. res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
  23. print(ip,"能使用")
  24. with open("ip.text",mode="a+") as f:
  25. f.write(":".join(ip)) #写入ip.text文本
  26. f.write("\n") #换行
  27. except Exception as e: #捕捉错误异常
  28. print(ip,"不能使用")

爬了一页,才几个能用,有3000多页,不可能手动的

00xx05---批量爬

  1. #!/usr/bin/env python3
  2. # coding:utf-8
  3. # 2019/11/18 22:38
  4. #lanxing
  5. import requests
  6. import re
  7. #防反爬
  8. headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36" }
  9. for i in range(1,3000): #爬3000个网页
  10. #网站
  11. url = "https://www.xicidaili.com/nn/{}".format(i)
  12. response = requests.get(url,headers=headers)
  13. # print(response.text)
  14. html = response.text
  15. # print(html)
  16. #re.S忽略换行的干扰
  17. ips = re.findall("<td>(\d+\.\d+\.\d+\.\d+)</td>",html,re.S)
  18. ports = re.findall(("<td>(\d+)</td>"),html,re.S)
  19. #print(ips)
  20. #print(ports)
  21. for ip in zip(ips,ports ): #提取拼接ip和端口
  22. print(ip)
  23. proxies = {
  24. "http":"http://" + ip[0] + ":" + ip[1],
  25. "https":"http://" + ip[0] + ":" + ip[1]
  26. }
  27. try:
  28. res = requests.get("http://www.baidu.com",proxies=proxies,timeout = 3) #访问网站等待3s没有反应,自动断开
  29. print(ip,"能使用")
  30. with open("ip.text",mode="a+") as f:
  31. f.write(":".join(ip)) #写入ip.text文本
  32. f.write("\n") #换行
  33. except Exception as e: #捕捉错误异常
  34. print(ip,"不能使用")

00xx06---最后

哈哈,感觉爬的速度太慢了,毕竟是单线程,如果要快速爬,可以试试用多线程爬取,

以后再补充完善代码吧

python-爬免费ip并验证其可行性的更多相关文章

  1. [python]爬代理ip v2.0(未完待续)

    爬代理ip 所有的代码都放到了我的github上面, HTTP代理常识 HTTP代理按匿名度可分为透明代理.匿名代理和高度匿名代理. 特别感谢:勤奋的小孩 在评论中指出我文章中的错误. REMOTE_ ...

  2. python爬取ip地址

    ip查询,异步get请求 分析接口,请求接口响应json 发现可以data中获取 result.json()['data'][0]['location'] # _*_ coding : utf-8 _ ...

  3. python爬取免费优质IP归属地查询接口

    python爬取免费优质IP归属地查询接口 具体不表,我今天要做的工作就是: 需要将数据库中大量ip查询出起归属地 刚开始感觉好简单啊,毕竟只需要从百度找个免费接口然后来个python脚本跑一晚上就o ...

  4. 爬取西刺网的免费IP

    在写爬虫时,经常需要切换IP,所以很有必要自已在数据维护库中维护一个IP池,这样,就可以在需用的时候随机切换IP,我的方法是爬取西刺网的免费IP,存入数据库中,然后在scrapy 工程中加入tools ...

  5. 无忧代理免费ip爬取(端口js加密)

    起因 为了训练爬虫技能(其实主要还是js技能-),翻了可能有反爬的网站挨个摧残,现在轮到这个网站了:http://www.data5u.com/free/index.shtml 解密过程 打开网站,在 ...

  6. 第二篇 - python爬取免费代理

    代理的作用参考https://wenda.so.com/q/1361531401066511?src=140 免费代理很多,但也有很多不可用,所以我们可以用程序对其进行筛选.以能否访问百度为例. 1. ...

  7. 爬取快代理的免费IP并测试

    各大免费IP的网站的反爬手段往往是封掉在一定时间内访问过于频繁的IP,因此在爬取的时候需要设定一定的时间间隔,不过说实话,免费代理很多时候基本都不能用,可能一千个下来只有十几个可以用,而且几分钟之后估 ...

  8. python 单例模式获取IP代理

    python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...

  9. Python获取免费的可用代理

    Python获取免费的可用代理 在使用爬虫多次爬取同一站点时,常常会被站点的ip反爬虫机制给禁掉,这时就能够通过使用代理来解决.眼下网上有非常多提供最新免费代理列表的站点.这些列表里非常多的代理主机是 ...

随机推荐

  1. 科普帖:深度学习中GPU和显存分析

    知乎的一篇文章: https://zhuanlan.zhihu.com/p/31558973 关于如何使用nvidia-smi查看显存与GPU使用情况,参考如下链接: https://blog.csd ...

  2. InsightFace源码以及pre-train模型以及使用

    一下摘自:https://blog.csdn.net/Fire_Light_/article/details/79602705 论文链接:ArcFace: Additive Angular Margi ...

  3. dubbo重连机制会不会造成错误

    dubbo在调用服务不成功时,默认会重试2次. Dubbo的路由机制,会把超时的请求路由到其他机器上,而不是本机尝试,所以 dubbo的重试机器也能一定程度的保证服务的质量. 但是如果不合理的配置重试 ...

  4. python 怎么像shell -x 一样追踪脚本运行过程

    python 怎么像shell -x 一样追踪脚本运行过程 [root@localhost keepalived]# python -m trace --trace mysql_start.py -- ...

  5. Jmeter插件:jp@gc - Dummy Sampler

    Dummy Sampler可以比较方便地模拟测试场景,自定义Request Data和Response Data 1. 安装插件:打开页面插件管理网站,下载plugins-manager.jar. 在 ...

  6. csps模拟87888990部分题解

    题面:https://www.cnblogs.com/Juve/articles/11752338.html https://www.cnblogs.com/Juve/articles/1175241 ...

  7. Ubuntu下github pages+hexo搭建自己的博客

    hexo 是一个基于Node.js的静态博客程序,可以方便的生成静态网页托管在github上.Hexo简单优雅, 而且风格多变, 适合搭建个人博客,而且支持多平台的搭建. 平台 Ubuntu14.04 ...

  8. LUOGU P3919 【模板】可持久化数组(主席树)

    传送门 解题思路 给每一时刻建一棵线段树维护当前时刻的值,然后修改的时候直接修改,查询的时候直接查,记住查询完后一定要复制. 代码 #include<iostream> #include& ...

  9. POJ-2752-Seek the Name-kmp的变形

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...

  10. IO初步,字节输入流和字节输出流

    字节输出流 OutputStream(基类,抽象) 特点:写任意的文件 方法:写出数据的方法:write write(int b) 写出1个字节 -128~127之间,写的是一个ASCLL码的值 wr ...