不要急于求成,你只要做的是比昨天的你更优秀一点
                       --匿名

今天给大家讲一下--IpProxy,由于从"http://www.xicidaili.com/nn"爬取,以下是我转载的博客

https://www.jianshu.com/p/8975a3997ab6

需要解决的问题

1.ip,端口和协议都是在静态页面中爬取
2.验证代理ip是否可用

这里就给大家看看爬取的代码怎么写,其他的配置可以看我之前的博客,具体代码可以进我的GitHub:。QAQ!!

# -*- coding: utf-8 -*-
import scrapy
from Iproxy.items import IproxyItem
import pdb
from Iproxy.settings import USER_AGENT
import re
from scrapy.linkextractors import LinkExtractor
import telnetlib class IproxySpider(scrapy.Spider):
name = 'iproxy'
allowed_domains = ['www.xicidaili.com']
start_urls = ['http://www.xicidaili.com/nn'] headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Host': 'www.xicidaili.com',
'Origin': 'www.xicidaili.com',
'Referer': 'http://www.xicidaili.com/',
'User-Agent': USER_AGENT,
'X-Requested-With': 'XMLHttpRequest',
} #验证ip代理是否可用
def telnet(self,item):
try:
telnetlib.Telnet(item['origin_ip'], port=item['port'], timeout=10.0)
except:
print('connect failure')
return False
else:
print('conncet success')
return True def parse(self, response):
iplist = IproxyItem()
sels = response.xpath('//tr[@class="odd"]')
items = {}
for sel in sels:
ips = sel.xpath('./td[2]').extract()[0].encode('utf8')
ports = sel.xpath('./td[3]').extract()[0].encode('utf8')
types = sel.xpath('./td[6]').extract()[0].encode('utf8')
type = re.findall(r'\>(.*?)\<',types)[0] #获取ip代理协议,低址,端口
if type == 'HTTP':
#items = 'http://' + re.findall(r'\>(.*?)\<',ips)[0] +':'+re.findall(r'\>(.*?)\<',ports)[0]
items['origin_ip'] = re.findall(r'\>(.*?)\<',ips)[0]
items['port'] = re.findall(r'\>(.*?)\<',ports)[0]
if self.telnet(items):
iplist['ip_name'] = 'http://' + re.findall(r'\>(.*?)\<',ips)[0]
iplist['port'] = re.findall(r'\>(.*?)\<',ports)[0] if type == 'HTTPS':
items['origin_ip'] = re.findall(r'\>(.*?)\<', ips)[0]
items['port'] = re.findall(r'\>(.*?)\<', ports)[0]
#items = 'https://' + re.findall(r'\>(.*?)\<', ips)[0] +':'+re.findall(r'\>(.*?)\<', ports)[0]
if self.telnet(items):
iplist['ip_name'] = 'https://' + re.findall(r'\>(.*?)\<',ips)[0]
iplist['port'] = re.findall(r'\>(.*?)\<', ports)[0] print iplist
yield iplist #获取页面链接url
links = LinkExtractor(restrict_css='div.pagination')
for link in links.extract_links(response):
yield scrapy.Request(link.url,callback=self.parse)

scrapy--ipproxy的更多相关文章

  1. Scrapy学习篇(十二)之设置随机IP代理(IPProxy)

    当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...

  2. Scrapy实战篇(五)之爬取历史天气数据

    本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...

  3. 大型分布式爬虫准备 scrapy + request

    那些高手 爬虫好文 而我避免这些问题的方式,控制台清除所有定时 var id = setInterval(function() {}, 0); while (id--) clearInterval(i ...

  4. Scrapy框架爬虫初探——中关村在线手机参数数据爬取

    关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...

  5. scrapy爬虫docker部署

    spider_docker 接我上篇博客,为爬虫引用创建container,包括的模块:scrapy, mongo, celery, rabbitmq,连接https://github.com/Liu ...

  6. scrapy 知乎用户信息爬虫

    zhihu_spider 此项目的功能是爬取知乎用户信息以及人际拓扑关系,爬虫框架使用scrapy,数据存储使用mongo,下载这些数据感觉也没什么用,就当为大家学习scrapy提供一个例子吧.代码地 ...

  7. ubuntu 下安装scrapy

    1.把Scrapy签名的GPG密钥添加到APT的钥匙环中: sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 6272 ...

  8. 网络爬虫:使用Scrapy框架编写一个抓取书籍信息的爬虫服务

      上周学习了BeautifulSoup的基础知识并用它完成了一个网络爬虫( 使用Beautiful Soup编写一个爬虫 系列随笔汇总 ), BeautifulSoup是一个非常流行的Python网 ...

  9. Scrapy:为spider指定pipeline

    当一个Scrapy项目中有多个spider去爬取多个网站时,往往需要多个pipeline,这时就需要为每个spider指定其对应的pipeline. [通过程序来运行spider],可以通过修改配置s ...

  10. scrapy cookies:将cookies保存到文件以及从文件加载cookies

    我在使用scrapy模拟登录新浪微博时,想将登录成功后的cookies保存到本地,下次加载它实现直接登录,省去中间一系列的请求和POST等.关于如何从本次请求中获取并在下次请求中附带上cookies的 ...

随机推荐

  1. ThinkPHP find大坑 不要随便用

    举例: M("User")->find(3); $m=M("User"); $m->userName="aaa"; $m-> ...

  2. mybatis SqlSession事务

    mybatis版本:3.4.6. mybatis默认的SqlSessionFactory是DefaultSqlSessionFactory,它openSession()的源码是: public Sql ...

  3. dom父节点

  4. 基于springmvc开发注解式ip拦截器

    一.注解类 @Documented @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) ...

  5. textarea存起来的数据把空格也存起来

    textarea的属性wrap="hard"可以把换行的内容也存起来. <html> <head> <title>这是一个小测试</tit ...

  6. 属性动画 常用属性及View常用方法

    View类中,常用于属性动画的属性: translationX and translationY: These properties control where the View is located ...

  7. 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用

    App->Activity->Custom Dialog 例子使用Activity 来实现自定义对话框 类CustomDialogActivity本身无任何特别之处.关键的一点是其在And ...

  8. APP常用检测

    检测设备.微信平台和app是否安装 // 检测是否安装了APP var isappinstalled = (function () { ); }()), // 检测ios设备 isIOS = (fun ...

  9. Google面试准备

    本人小弱,面试过了Google的HC,虽然team match还没完成,到最后还有变数.但对自己这段时间的努力,也算一个交代了. 最初是一年半前Google的HR联系到我,然后第一次在电面就挂了.经过 ...

  10. GNU Nano编辑器

    ^表示 control建 ^X 退出 nano ^O 保存文件 ^R 插入其他文件内容至光标位置 ^W 查找字符串 ^Y 跳至前一屏 ^V 跳至后一屏 ^K 剪切光标所在行并保存到剪贴板,或剪切选中内 ...