python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程。

爬虫的初级阶段,添加headers和ip代理可以解决很多问题。

贴代码:说下思路

1、到http://www.xicidaili.com/nn/抓取相应的代理ip地址,地址比较多,但是不保证能用。先保存到列表

2、多线程验证代理ip的可行性,然后写入到对应的txt文件

3、当需要代理ip的时候,倒入模块,执行main()函数,可得到可用的代理ip进行后续功能。

验证ip用到了telnetlib和requests两种方法。建议要爬取哪个网页,直接requests相应网页验证比较好。

#coding:utf-8

from bs4 import BeautifulSoup
import time
import threading
import random
import telnetlib,requests #设置全局超时时间为3s,也就是说,如果一个请求3s内还没有响应,就结束访问,并返回timeout(超时)
import socket
socket.setdefaulttimeout(3) headers = {
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36",
} def get_ip():
#获取代理IP,返回列表
httpResult=[]
httpsResult=[]
try:
for page in range(1,2):
IPurl = 'http://www.xicidaili.com/nn/%s' %page
rIP=requests.get(IPurl,headers=headers)
IPContent=rIP.text
print IPContent
soupIP = BeautifulSoup(IPContent,'lxml')
trs = soupIP.find_all('tr')
for tr in trs[1:]:
tds = tr.find_all('td')
ip = tds[1].text.strip()
port = tds[2].text.strip()
protocol = tds[5].text.strip()
if protocol == 'HTTP':
httpResult.append( 'http://' + ip + ':' + port)
elif protocol =='HTTPS':
httpsResult.append( 'https://' + ip + ':' + port)
except:
pass
return httpResult,httpsResult
'''
#验证ip地址的可用性,使用telnetlib模块_http
def cip(x,y):
f = open("E:\ip_http.txt","a")
f.truncate()
try:
telnetlib.Telnet(x, port=y, timeout=5)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n')
#验证ip地址的可用性,使用telnetlib模块_https
def csip(x,y):
f = open("E:\ip_https.txt","a")
f.truncate()
try:
telnetlib.Telnet(x, port=y, timeout=5)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n')
''' #验证ip地址的可用性,使用requests模块,验证地址用相应要爬取的网页 http
def cip(x,y):
f = open("E:\ip_http.txt","a")
f.truncate()
try:
print (x+y)
requests.get('http://ip.chinaz.com/getip.aspx',proxies={'http':x+":"+y},timeout=3)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n')
#验证ip地址的可用性,使用requests模块,验证地址用相应要爬取的网页。https
def csip(x,y):
f = open("E:\ip_https.txt","a")
f.truncate()
try:
print (x+y)
requests.get('https://www.lagou.com/',proxies={'https':x+":"+y},timeout=3)
except:
print('f')
else:
print('---------------------------success')
f.write(x+':'+y+'\n') def main():
httpResult,httpsResult = get_ip() threads = []
open("E:\ip_http.txt","a").truncate()
for i in httpResult:
a = str(i.split(":")[-2][2:].strip())
b = str(i.split(":")[-1].strip())
t = threading.Thread(target=cip,args=(a,b,))
threads.append(t) for i in range(len(httpResult)):
threads[i].start()
for i in range(len(httpResult)):
threads[i].join() threads1 = []
open("E:\ip_https.txt","a").truncate()
for i in httpsResult:
a = str(i.split(":")[-2][2:].strip())
b = str(i.split(":")[-1].strip())
t = threading.Thread(target=csip,args=(a,b,))
threads1.append(t) for i in range(len(httpsResult)):
threads1[i].start()
for i in range(len(httpsResult)):
threads1[i].join() if __name__ == '__main__':
main()

python——代理ip获取的更多相关文章

  1. 爬虫的新手使用教程(python代理IP)

    前言 Python爬虫要经历爬虫.爬虫被限制.爬虫反限制的过程.当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程.爬虫的初级阶段,添加headers和ip代理可以解决很多问题. ...

  2. c# 代理IP获取通用方法

    调用: ConcurrentQueue<string> proxyIpQueue = new ConcurrentQueue<string>(); Grab_ProxyIp(p ...

  3. python通过ip获取地址

    # -*- coding: utf-8 -*- url = "http://ip.taobao.com/service/getIpInfo.php?ip=" #查找IP地址 def ...

  4. PYTHON代理IP

    import urllib.request url = 'http://www.whatismyip.com.tw/' proxy_support = urllib.request.ProxyHand ...

  5. 使用TaskManager爬取2万条代理IP实现自动投票功能

    话说某天心血来潮想到一个问题,朋友圈里面经常有人发投票链接,让帮忙给XX投票,以前呢会很自觉打开链接帮忙投一票.可是这种事做多了就会考虑能不能使用工具来进行投票呢,身为一名程序猿决定研究解决这个问题. ...

  6. 写了个小爬虫,为何用上代理ip总是出现错误。

    import urllib.request import re import os import random import threading def url_open(url): #在第8到第12 ...

  7. 代理 IP 云打码平台的使用

    代理ip 获取代理ip的网站: 快代理 西祠代理 www.goubanjia.com #代理ip import requests headers = { 'User-Agent':'Mozilla/5 ...

  8. python爬虫实战(一)——实时获取代理ip

    在爬虫学习的过程中,维护一个自己的代理池是非常重要的. 详情看代码: 1.运行环境 python3.x,需求库:bs4,requests 2.实时抓取西刺-国内高匿代理中前3页的代理ip(可根据需求自 ...

  9. python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...

随机推荐

  1. 01 Python 基础数据类型

    基础数据类型,有7种类型,存在即合理. 1.int 整数 主要是做运算的 .比如加减乘除,幂,取余  + - * / ** %...2.bool 布尔值 判断真假以及作为条件变量3.str 字符串 存 ...

  2. P3084 [USACO13OPEN]照片Photo dp

    题意: 有n个区间,每个区间只能有一个斑点奶牛,问最多有几个斑点奶牛. 思路: 首先要处理出每个点的L[i],R[i]. L[i]表示L[i]-i-1之间一定有一个点.i也是选中的. R[i]表示R[ ...

  3. Codeforces Technocup 2017 - Elimination Round 2 E Subordinates(贪心)

    题目链接 http://codeforces.com/contest/729/problem/E 题意:给你n个人,主管id为s,然后给你n个id,每个id上对应一个数字表示比这个人大的有几个. 最后 ...

  4. 深入理解 Java 中 SPI 机制

    本文首发于 vivo互联网技术 微信公众号 链接:https://mp.weixin.qq.com/s/vpy5DJ-hhn0iOyp747oL5A作者:姜柱 SPI(Service Provider ...

  5. centos 6.8 下没有yum命令解决方法(报错: -bash: yum: command not found)

    1.去 http://mirrors.163.com/centos/6/os/x86_64/Packages/ 地址下载4个rpm安装包:python-iniparse-0.3.1-2.1.el6.n ...

  6. Java 中的array数组总结之一

    数组:是一个将同种类型的数据存储在存储单元中. 可以用三种方式声明数组: 1.数据类型 标识符[]; int mothDays[]; 2.数据类型 标识符[] = new 数据类型[大小]; int ...

  7. 锁和synchronized

    锁的常见概念 互斥: 同一时刻只有一个线程执行 临界区:一段需要互斥执行的代码 细粒度锁: 用不同的锁对受保护资源进行精细化管理. 细粒度锁可以提高并行度,是性能优化的一个重要手段 死锁 :一组互相竞 ...

  8. PythonI/O进阶学习笔记_4.自定义序列类(序列基类继承关系/可切片对象/推导式)

    前言: 本文代码基于python3 Content: 1.python中的序列类分类 2. python序列中abc基类继承关系 3. 由list的extend等方法来看序列类的一些特定方法 4. l ...

  9. java对象与java对象引用的区别

    java对象与java对象引用的区别 对象与对象引用的区别 直接用例子说话吧 Person per = new Person("张三"); 这一条语句,其实包括了四个动作: 右边的 ...

  10. charles 禁用Cookies /Block Cookies Settings

    本文参考:charles 禁用Cookies 禁用cookies/Block Cookies Settings 功能:阻止发送和接收Cookie 禁用Cookie工具 禁用Cookie工具阻止发送和接 ...