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

今天给大家讲一下--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. linux程序分析工具介绍(一)—-”/proc”

    写在最前面:在开始本文之前,笔者认为先有必要介绍一下linux下的man,如果读者手头用linux系统,直接在终端输入man man便可以看到详细的说明,我在这里简单的总结一下,man命令是用来查看l ...

  2. ToDictionary写法

    把List集合转化成Dictionary public ActionResult Dimo() { Dictionary<string, Object> param = new Dicti ...

  3. Hibernate课程 初探一对多映射3-3 单向多对一的测试

    public static void testManyToOne(){ Student stu1 = new Student("小明","男"); Studen ...

  4. Differences or similarities between Java and C++

    “作为一名C++程序员,我们早已掌握了面向对象Object-oriented Programming程序设计的基本概念,而且Java的语法无疑是非常熟悉的.事实上,Java本来就是从C++衍生出来的. ...

  5. C++ Knowledge series Inheritance & RTTI & Exception Handling

    Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...

  6. poi读取excel的辅助类

    补充:对于这个工具已经转为一个工程项目,采用的是saxreader方式,支持大数据文件的读取.具体可以参照  github上的源码,使用可以简单参照wiki.项目wiki地址https://git.o ...

  7. 从PeopleEditor控件中取出多用户并更新到列表

    如果一个列表中有一个字段类型为用户或用户组,并且设置为用户,允许多值的话,那么用代码进行更新的时候就必须将这个字段的值赋成SPFieldUserValueCollection类型,以下代码即为从Peo ...

  8. sudoer解释

    /etc/sudoer ## Sudoers allows particular users to run various commands as ## the root user, without ...

  9. April 5 2017 Week 14 Wednesday

    Today is a perfect day to start living your dream. 实现梦想,莫如当下. Miracles may happen every day. If you ...

  10. 【转载】#370 - Subscribe to an Event by Adding an Event Handle

    You subscribe to a particular event in C# by defining an event handler-code that will be called when ...