2017.08.05 Python网络爬虫实战之获取代理

1.项目准备:爬取网站:http://www.proxy360.cn/Region/China,http://www.xicidaili.com/

2.创建编辑Scrapy爬虫:

scrapy startproject getProxy

scrapy genspider proxy360Spider proxy360.cn

项目目录结构:

 

3.修改items.py:

 

 4.修改Spider.py文件 proxy360Spider.py:

(1)先使用scrapy shell命令查看一下连接网络返回的结果和数据:

scrapy shell http://www.proxy360.cn/Region/China

(2)再看一下response的数据内容:response.xpath('/*').extract(),返回的数据中含有代理服务器;

(3)观察发现所有的数据模块都是以<div class="proxylistitem" name="list_proxy_ip">这个tag开头的:

(4)在scrapy shell中测试一下:

subSelector=response.xpath('//div[@class="proxylistitem" and @name="list_proxy_ip"]')

subSelector.xpath('.//span[1]/text()').extract()[0]

 subSelector.xpath('.//span[2]/text()').extract()[0]

 subSelector.xpath('.//span[3]/text()').extract()[0]

 subSelector.xpath('.//span[4]/text()').extract()[0]

 

(5) 编写Spider文件 proxy360Spider.py:

# -*- coding: utf-8 -*-
import scrapy
from getProxy.items import GetproxyItem

class Proxy360spiderSpider(scrapy.Spider):
name = 'proxy360Spider'
allowed_domains = ['proxy360.cn']

nations=['Brazil','China','Amercia','Taiwan','Japan','Thailand','Vietnam','bahrein']
start_urls=[ ]
for nation in nations:
start_urls.append('http://www.proxy360.cn/Region/'+nation)

def parse(self, response):
subSelector=response.xpath('//div[@class="proxylistitem" and @name="list_proxy_ip"]')
items=[]
for sub in subSelector:
item=GetproxyItem()
item['ip']=sub.xpath('.//span[1]/text()').extract()[0]
item['port']=sub.xpath('.//span[2]/text()').extract()[0]
item['type']=sub.xpath('.//span[3]/text()').extract()[0]
item['loction']=sub.xpath('.//span[4]/text()').extract()[0]
item['protocol']='HTTP'
item['source']='proxy360'
items.append(item)
return items



(6)修改pipelines.py文件,处理:
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html


class GetproxyPipeline(object):
def process_item(self, item, spider):
fileName='proxy.txt'
with open(fileName,'a') as fp:
fp.write(item['ip'].encode('utf8').strip()+'\t')
fp.write(item['port'].encode('utf8').strip()+'\t')
fp.write(item['protocol'].encode('utf8').strip()+'\t')
fp.write(item['type'].encode('utf8').strip()+'\t')
fp.write(item['loction'].encode('utf8').strip()+'\t')
fp.write(item['source'].encode('utf8').strip()+'\n')
return item


(7)修改Settings.py,决定由哪个文件来处理获取的数据:

(8)执行结果:

 

5.多个Spider,只有一个Spdier的时候得到的proxy数据不够多:

(1 )到getProxy目录下,执行:scrapy  genspider xiciSpider xicidaili.com

(2)确定如何获取数据:scrapy shell http://www.xicidaili.com/nn/2

 (3)只需要在settings.py中添加一个USER_Agent项就可以了

再次测试如何获取数据:scrapy shell http://www.xicidaili.com/nn/2

(4)在浏览器中查看源代码:发现所需的数据块都是以<tr class="odd">开头的

(5)在scrapy shell中执行命令:

 subSelector=response.xpath('//tr[@class=""]| //tr[@class="odd"]')

subSelector[0].xpath('.//td[2]/text()').extract()[0]

 subSelector[0].xpath('.//td[3]/text()').extract()[0]

 subSelector[0].xpath('.//td[4]/a/text()').extract()[0]

subSelector[0].xpath('.//td[5]/text()').extract()[0]

subSelector[0].xpath('.//td[6]/text()').extract()[0]

 

(6)编写xiciSpider.py:

# -*- coding: utf-8 -*-
import scrapy
from getProxy.items import GetproxyItem

class XicispdierSpider(scrapy.Spider):
name = 'xiciSpdier'
allowed_domains = ['xicidaili.com']
wds=['nn','nt','wn','wt']
pages=20
start_urls=[]
for type in wds:
for i in xrange(1,pages+1):
start_urls.append('http://www.xicidaili.com/'+type+'/'+str(i))


def parse(self, response):
subSelector=response.xpath('//tr[@class=""]| //tr[@class="odd"]')
items=[]
for sub in subSelector:
item=GetproxyItem()
item['ip']=sub.xpath('.//td[2]/text()').extract()[0]
item['port']=sub.xpath('.//td[3]/text()').extract()[0]
item['type']=sub.xpath('.//td[5]/text()').extract()[0]
if sub.xpath('.//td[4]/a/text()'):
item['loction']=sub.xpath('.//td[4]/a/text()').extract()[0]
else:
item['loction']=sub.xpath('.//td[4]/text()').extract()[0]

item['protocol']=sub.xpath('.//td[6]/text()').extract()[0]
item['source']='xicidaili'
items.append(item)


return items

 (7)执行:scrapy crawl xiciSpider

结果:

 

 

6.验证获取的代理服务器地址是否可用:另外写一个python程序验证代理:testProxy.py

#! /usr/bin/env python
# -*- coding: utf-8 -*-


import urllib2
import re
import threading

class TesyProxy(object):
def __init__(self):
self.sFile=r'proxy.txt'
self.dFile=r'alive.txt'
self.URL=r'http://www.baidu.com/'
self.threads=10
self.timeout=3
self.regex=re.compile(r'baidu.com')
self.aliveList=[]

self.run()

def run(self):
with open(self.sFile,'r') as fp:
lines=fp.readlines()
line=lines.pop()
while lines:
for i in xrange(self.threads):
t=threading.Thread(target=self.linkWithProxy,args=(line,))

t.start()
if lines:
line=lines.pop()
else:
continue

with open(self.dFile,'w') as fp:
for i in xrange(len(self.aliveList)):
fp.write(self.aliveList[i])

def linkWithProxy(self,line):
lineList=line.split('\t')
protocol=lineList[2].lower()
server=protocol+r'://'+lineList[0]+':'+lineList[1]
opener=urllib2.build_opener(urllib2.ProxyHandler({protocol:server}))
urllib2.install_opener(opener)
try:
response=urllib2.urlopen(self.URL,timeout=self.timeout)
except:
print('%s connect failed' %server)
return
else:
try:
str=response.read()
except:
print('%s connect failed' %server)
return
if self.regex.search(str):
print('%s connect success ............' %server)
self.aliveList.append(line)


if __name__ == '__main__':
TP=TesyProxy()


执行命令:python testProxy.py
结果:

 

 
 

 

转载于:https://www.cnblogs.com/hqutcy/p/7291191.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据数据解析:将请求下来的数据进行过滤,提取我们想要的数据数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值