Python爬取代理ip
# -*- coding:utf-8 -*-
#author : willowj
import urllib
import urllib2
from bs4 import BeautifulSoup
import re
import bs4 import sys reload(sys)
sys.setdefaultencoding('utf8') def ip_test(ip,url= "https://www.baidu.com"):
#test ip if can be used
#url = "http://ip.chinaz.com/getip.aspx"# 默认测试网址
ip1="http://"+ip
try :
res = urllib.urlopen(url,proxies={'http:':ip1}).read() #尝试代理访问
print 'ok',ip1 #,res
return True
except Exception,e:
print "failed"
return False def get_iphtml_inyoudaili():
url='http://www.youdaili.net'
html=urllib2.urlopen(url)
code=html.read()
#href="http://www.youdaili.net/Daili/http/26672.html" title="12月27号 最新代理http服务器ip地址"
regexp='href="(.*?)" .*?最新代理http服务器ip地址'
pat=re.compile(regexp)
met=re.findall(pat,code)
print met[0]
#最新代理http服务器ip地址 html
return met[0] def getIps(url):
#getip from website, test,and return,save aviable ips in 'ips.txt'
htmlip=urllib2.urlopen(url)
codeip=htmlip.read() regexpip='([1-9][0-9]{0,2}\.\S*?)@HTTP#' #IP样式
pat_ip=re.compile(regexpip) met_ip=re.findall(pat_ip,codeip) ips=[]
file_open=open('ips.txt','w')
for x in met_ip:
print x
if ip_test(x):
ips.append(x)
file_open.write(x+'\n')
file_open.close()
#print ips,'youdaili'
return ips def saveIps(list):
file_open=open('ips.txt','w')
for ip in list:
file_open.write(ip+'\n')
file_open.close() def read_ips(file='ips.txt'):
'''读取IP 以list返回'''
file_open=open(file)
lines=file_open.readlines()
ips=[]
for line in lines:
ip=line.strip("\n")
ips.append(ip)
print ips
return ips if __name__=="__main__": ips = getIps(get_iphtml_inyoudaili()) saveIps(ips)
Python爬取代理ip的更多相关文章
- 使用Python爬取代理ip
本文主要代码用于有代理网站http://www.kuaidaili.com/free/intr中的代理ip爬取,爬虫使用过程中需要输入含有代理ip的网页链接. 测试ip是否可以用 import tel ...
- python爬虫爬取代理IP
# #author:wuhao # #--*------------*-- #-****#爬取代理IP并保存到Excel----#爬取当日的代理IP并保存到Excel,目标网站xicidaili.co ...
- python代理池的构建3——爬取代理ip
上篇博客地址:python代理池的构建2--代理ip是否可用的处理和检查 一.基础爬虫模块(Base_spider.py) #-*-coding:utf-8-*- ''' 目标: 实现可以指定不同UR ...
- python 批量爬取代理ip
import urllib.request import re import time import random def getResponse(url): req = urllib.request ...
- 爬虫爬取代理IP池及代理IP的验证
最近项目内容需要引入代理IP去爬取内容. 为了项目持续运行,需要不断构造.维护.验证代理IP. 为了绕过服务端对IP 和 频率的限制,为了阻止服务端获取真正的主机IP. 一.服务器如何获取客户端IP ...
- 自动爬取代理IP例子
import time import json import datetime import threading import requests from lxml import etree from ...
- 爬取代理IP
现在爬虫好难做啊,有些网站直接封IP,本人小白一个,还没钱,只能找免费的代理IP,于是去爬了西刺免费代理,结果技术值太低,程序还没调试好, IP又被封了... IP又被封了... IP又被封了... ...
- 爬取代理IP,并判断是否可用。
# -*- coding:utf-8 -*- from gevent import monkey monkey.patch_all() import urllib2 from gevent.pool ...
- 原创:Python爬虫实战之爬取代理ip
编程的快乐只有在运行成功的那一刻才知道QAQ 目标网站:https://www.kuaidaili.com/free/inha/ #若有侵权请联系我 因为上面的代理都是http的所以没写这个判断 代 ...
随机推荐
- 使用GDB调试Go语言
用Go语言已经有一段时间了,总结一下如何用GDB来调试它! ps:网上有很多文章都有描述,但是都不是很全面,这里将那些方法汇总一下 GDB简介 GDB是GNU开源组织发布的⼀一个强⼤大的UNIX下的 ...
- 久违的问候-----eclipse中搭建maven项目2016年
好久没有写过博客了,可是一直向别人推荐自己的博客,深感惭愧!今天再次在寒冷之夜继续code,config,write. 接下来,我们就来谈下eclipse中搭建maven web工程的步骤!虽然就是一 ...
- J2EE项目修改编码问题
一:项目编码修改 新建项目后,右键项目-->Properties-->Resource,这时可以修改编码为UTF-8. 二:JSP页面编码修改 Window --> Preferen ...
- Oracle 把秒转成时分秒格式(hh24:mm:ss);检测字符串是否是数字;字符串转换为数字
不说废话,贴代码: CREATE OR REPLACE FUNCTION to_time(sec IN NUMBER) RETURN VARCHAR2 IS /*把秒转成时分秒格式 auth lzpo ...
- C#实现哥德巴赫猜想
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Goet ...
- Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码)
1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过开发一款我们自己的APP.本人算是一个基于.net的GIS开发入门者(马上就大四啦), 暑假在学校参加GIS比赛有大把 ...
- 在usercontrol里实现导航
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/NewContent.xaml&qu ...
- Android之计算缓存大小并且清空缓存
转载博客:http://www.2cto.com/kf/201503/385492.html 项目中碰到了计算缓存大小和清空缓存的功能,这个很常见的功能,几乎每个APP都有,以为实现很简单,网上搜了一 ...
- jquery EasyUI的formatter格式化函数代码
要格式化数据表格列,需要设置formatter属性,该属性是一个函数,它包含两个参数: value: 对应字段的当前列的值 record: 当前行的记录数据 复制代码 代码如下: $('#tt' ...
- Deque的部分成员函数 解析,关于这个类,百度有很多解析,唯独没有其函数介绍
函数 描述 c.assign(beg,end) c.assign(n,elem) 将[beg; end)区间中的数据赋值给c. 将n个elem的拷贝赋值给c. c.at(idx) 传回索引idx所指的 ...