一年前突然有个灵感,想搞个强大的网盘搜索引擎,但由于大学本科学习软件工程偏嵌入式方向,web方面的能力有点弱,不会jsp,不懂html,好久没有玩过sql,但就是趁着年轻人的这股不妥协的劲儿,硬是把以前没有学习的全部给学了一遍,现在感觉web原来也就那么回事。好了,废话就不说了,看到本文的读者,可以先看看我做的东西:去转盘网

  ok搜搜:www.oksousou.com(这个是磁力,顺便拿出来给大伙观赏)

  言归正传,由于我要爬取百度网盘,而度娘你懂的的搞爬虫出生的,反爬虫的能力很牛掰。尤其像我用我的电脑去爬百度网盘,爬几天百度就盯上了我的机子,爬虫开始爬不出东西。之后网上东查,西查,发现可以通过代理来解决这个问题,所以又去爬代理。我爬的是这个网站:

http://www.xicidaili.com/ 之后他貌似他开始反击,我又将魔爪指向了:http://www.kuaidaili.com。

想必看这篇博文的多半是程序猿,所以还是先上代码(我会写注释的,放心,该爬虫以http://www.xicidaili.com/为目标):

#coding:utf-8

import json

import sys

import urllib, urllib2

import datetime

import time

reload(sys)

sys.setdefaultencoding('utf-8') 

from Queue import Queue

from bs4 import BeautifulSoup

import MySQLdb as mdb

DB_HOST = '127.0.0.1'

DB_USER = 'root'

DB_PASS = 'root'

ID=0

ST=1000

uk='3758096603'

classify="inha"

proxy = {u'https':u'118.99.66.106:8080'}

class ProxyServer:

def __init__(self): #这个就不说了,数据库初始化,我用的是mysql

self.dbconn = mdb.connect(DB_HOST, DB_USER, DB_PASS, 'ebook', charset='utf8')

self.dbconn.autocommit(False)

self.next_proxy_set = set()

self.chance=0

self.fail=0

self.count_errno=0

self.dbcurr = self.dbconn.cursor()

self.dbcurr.execute('SET NAMES utf8')

def get_prxy(self,num): #这个函数用来爬取代理

while num>0:

global proxy,ID,uk,classify,ST

count=0

for page in range(1,718): #代理网站总页数,我给了个718页

if self.chance >0: #羊毛出在羊身上,如过爬取网站开始反击我,我就从他那爬下来的

代理伪装,这个self.chance表示我什么时候开始换代理

if ST % 100==0:

self.dbcurr.execute("select count(*) from proxy")

for r in self.dbcurr:

count=r[0]

if ST>count:

ST=1000 #我是从数据库的第1000条开始换的,这段你可以改,搞个随机函数随机换,我写的很简单

self.dbcurr.execute("select * from proxy where ID=%s",(ST))

results = self.dbcurr.fetchall()

for r in results:

protocol=r[1]

ip=r[2]

port=r[3]

pro=(protocol,ip+":"+port)

if pro not in self.next_proxy_set:

self.next_proxy_set.add(pro)

self.chance=0

ST+=1

proxy_support = urllib2.ProxyHandler(proxy) #注册代理

# opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler(debuglevel=1))

opener = urllib2.build_opener(proxy_support)

urllib2.install_opener(opener)

#添加头信息,模仿浏览器抓取网页,对付返回403禁止访问的问题

# i_headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}

i_headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48'}

#url='http://www.kuaidaili.com/free/inha/' + str(page)

url='http://www.kuaidaili.com/free/'+classify+'/' + str(page)

html_doc=""

try:

req = urllib2.Request(url,headers=i_headers)

response = urllib2.urlopen(req, None,5)

html_doc = response.read() #这不就获取了要爬取的页面嘛?

except Exception as ex: #看抛出异常了,可能开始反击我,我开始换代理

print "ex=",ex

pass

self.chance+=1

if self.chance>0:

if len(self.next_proxy_set)>0:

protocol,socket=self.next_proxy_set.pop()

proxy= {protocol:socket}

print "proxy",proxy

print "change proxy success."

continue

#html_doc = urllib2.urlopen('http://www.xici.net.co/nn/' + str(page)).read()

if html_doc !="": #解析爬取的页面,用的beautifulSoup

soup = BeautifulSoup(html_doc,from_encoding="utf8")

#print "soup",soup

#trs = soup.find('table', id='ip_list').find_all('tr') #获得所有行

trs = ""

try:

trs = soup.find('table').find_all('tr')

except:

print "error"

continue

for tr in trs[1:]:

tds = tr.find_all('td')

ip = tds[0].text.strip() #ip

port = tds[1].text.strip() #端口

protocol = tds[3].text.strip()

#tds = tr.find_all('td')

#ip = tds[2].text.strip()

#port = tds[3].text.strip()

#protocol = tds[6].text.strip()

get_time= tds[6].text.strip()

#get_time = "20"+get_time

check_time = datetime.datetime.strptime(get_time,'%Y-%m-%d %H:%M:%S')

temp = time.time()

x = time.localtime(float(temp))

time_now = time.strftime("%Y-%m-%d %H:%M:%S",x) # get time now,入库时间

http_ip = protocol+'://'+ip+':'+port

if protocol == 'HTTP' or protocol == 'HTTPS': #只要http协议相关代理,其他一律不要

content=""

try: #我就是不放心这个网站,所以爬下来后我又开始检测代理是否真的有效

proxy_support=urllib2.ProxyHandler({protocol:http_ip})

# proxy_support = urllib2.ProxyHandler({'http':'http://124.200.100.50:8080'})

opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)

urllib2.install_opener(opener)

if self.count_errno>50:

self.dbcurr.execute("select UID from visited where ID=%s",(ID)) #这是我的数据库,我取了一个叫uk的东东,这个

你不用管,你想检测拿你要爬取的链接检测代理吧

for uid in self.dbcurr:

uk=str(uid[0])

ID+=1

if ID>50000:

ID=0

self.count_errno=0

test_url="http://yun.baidu.com/pcloud/friend/getfanslist?start=0&query_uk="+uk+"&limit=24" #我用来检测的链接

print "download:",http_ip+">>"+uk

req1 = urllib2.Request(test_url,headers=i_headers)

response1 = urllib2.urlopen(req1, None,5)

content = response1.read()

except Exception as ex: #抛异常后的处理

#print "ex2=",ex

pass

self.fail+=1

if self.fail>10:

self.fail=0

break

continue

if content!="": 

json_body = json.loads(content)    

errno = json_body['errno']  

self.count_errno+=1 

if errno!=-55: #检验该代理是有用的,因为content!="" 并且度娘返回not -55

print "success."

self.dbcurr.execute('select ID from proxy where IP=%s', (ip)) #开始入库了

y = self.dbcurr.fetchone()

if not y:

print 'add','%s//:%s:%s' % (protocol, ip, port)

self.dbcurr.execute('INSERT INTO proxy(PROTOCOL,IP,PORT,CHECK_TIME,ACQ_TIME) VALUES(%s,%s,%s,%s,%s)',(protocol,ip,port,check_time,time_now))

self.dbconn.commit()

num-=1

if num % 4 ==0:

classify="intr" #这个是原来网站的那几个标签栏名称,我是一栏一栏的爬取的

if num % 4 ==1:

classify="outha"

if num % 4 ==2:

classify="outtr"

if num % 4 ==3:

classify="inha"

  if __name__ == '__main__':

  proSer = ProxyServer()

  proSer.get_prxy(10000) #爬10000次,单线程,爬个1两周没有问题

  

以上就是本人的代理爬虫代码,有兴趣可以加我qq:3047689758,还可以去:www.quzhuanpan.com首页关注我们的微博(如果你没有看到微博,可能是新代码我没有推),谢谢阅读,欢迎转载。

怎么来爬取代理服务器ip地址?的更多相关文章

  1. python爬虫爬取代理IP

    # #author:wuhao # #--*------------*-- #-****#爬取代理IP并保存到Excel----#爬取当日的代理IP并保存到Excel,目标网站xicidaili.co ...

  2. C#取真实IP地址及分析

    说一哈,我也是转来的,不是想骗PV,方便自己查而已! 目前网上流行的所谓"取真实IP地址"的方法,都有bug,没有考虑到多层透明代理的情况. 多数代码类似: string IpAd ...

  3. 爬虫爬取代理IP池及代理IP的验证

    最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...

  4. python 批量爬取代理ip

    import urllib.request import re import time import random def getResponse(url): req = urllib.request ...

  5. 使用Python爬取代理ip

    本文主要代码用于有代理网站http://www.kuaidaili.com/free/intr中的代理ip爬取,爬虫使用过程中需要输入含有代理ip的网页链接. 测试ip是否可以用 import tel ...

  6. 自动爬取代理IP例子

    import time import json import datetime import threading import requests from lxml import etree from ...

  7. python 爬虫入门1 爬取代理服务器网址

    刚学,只会一点正则,还只能爬1页..以后还会加入测试 #coding:utf-8 import urllib import urllib2 import re #抓取代理服务器地址 Key = 1 u ...

  8. python代理池的构建3——爬取代理ip

    上篇博客地址:python代理池的构建2--代理ip是否可用的处理和检查 一.基础爬虫模块(Base_spider.py) #-*-coding:utf-8-*- ''' 目标: 实现可以指定不同UR ...

  9. c# 取本地ip地址

    public static System.Net.IPAddress[] GetIpAddress() { string hostName = System.Net.Dns.GetHostName() ...

随机推荐

  1. 搭建开源入侵检测系统Snort并实现与防火墙联动

    Snort作为一款优秀的开源主机入侵检测系统,在windows和Linux平台上均可安装运行.BT5作为曾经的一款经典的渗透神器,基于 Ubuntu,里面已经预装很多的应用,比如Mysql.Apach ...

  2. 流畅的python第二章序列构成的数组学习记录

    python内置序列类型概览 列表推导和生成器表达式 列表推导是构建列表的快捷方式,而生成器表达式可以用来创建其他任何类型的序列 列表推导的示例 >>>test = [i*2 for ...

  3. 解决 java.lang.ClassNotFoundException配置文件出错的问题

    出现的原因: 1.jar包没有导入 2.jar包有冲突 3.jar包没有同步发布到自己项目的lib目录中 解决方案: maven构建工程的方式:项目点击右键 点击 Properties 选择Deplo ...

  4. [TypeScript] Work with DOM Elements in TypeScript using Type Assertions

    The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...

  5. java调用机器上的shell脚本

    java调用机器上的shell脚本,可以这样方便的通过shell脚本调用本机的C.C++等程序 Process process = null; Runtime runTime = Runtime.ge ...

  6. .Net Framework 之 托管代码和非托管代码的区别

    一.什么是托管代码(managed code)? 托管代码:是微软的中间语言(IL),即微软中间语言MSIL(Microsoft Interspace Language). “ 源代码→机器码 ”运行 ...

  7. java程序main方法的参数String[] args

    public class ArgsTest { public static void main(String[] args) { System.out.println(args.length); fo ...

  8. Android 8.0新特性-取消大部分静态注册广播

    今天楼主在写一个广播的demo,功能非常的简单,就是一个应用发送一个自定义的广播,同时在这个应用里面定义了一个广播接受者,并且在AndroidManifest文件中进行静态的注册.Demo看上去非常的 ...

  9. Mybatis学习记录(六)--开发中的小问题

    近期開始做项目,期间遇到一些小问题,开此贴记录一下 1.关于order by 今天写一个sql查询语句,用了order by可是一直没效果,后来才发现用了#{}取值,mybatis使用这个的话对于St ...

  10. Sphinx-实战

    配置完成后, 有了数据源.索引文件存储位置等, 便可以使用 indexer 工具创建索引, 收集要被检索的数据 -c 指定配置文件 默认使用 etc/sphinx.conf --all 对所有索引重新 ...