实例介绍
【实例简介】利用Scrapy框架爬取免费的的西刺代理IP
内含模拟浏览器,利用代理池反反爬虫
【实例截图】
【核心代码】pipelines.py
from urllib.request import ProxyHandler, build_opener
import re
from urllib import request
''' 本段注释代码,将获取的信息保存到MongoDB数据库
client = MongoClient('localhost', 27017)
db_auth = client.admin db_auth.authenticate("root", "123")
db = client['代理'] collection = db['IP代理'] '''
'''以文本方式保存到本地''' f = open('可用代理.txt','w',encoding='utf-8')
class ProxyPipeline(object):
def process_item(self, item, spider):
for i in range(0,len(item["IP"])):
#print(item['name'][i])
proxy = item['IP'][i] ':' item["port"][i]
proxy_handler = ProxyHandler({'http': 'http://' proxy}) opener = build_opener(proxy_handler)
try: head = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', } url = 'http://www.xdaili.cn/monitor'
req = request.Request(url, headers=head) response = opener.open(req) data = response.read().decode('utf-8') # print(data)
if data:
'''保存到数据库 ret = collection.find_one({'IP': item['IP'][i], '端口': item["port"][i]})
if ret: # print('已经存在了') pass
else: collection.save({'IP': item['IP'][i], '端口': item['port'][i], '匿名度': item['nmd'][i], '类型': item['type'][i], '位置': item['addr'][i], '最后验证时间': item['lastime'][i]}) # 向数据库插入一条记录 '''
f.write( proxy) f.write("\n")
print(proxy)
except Exception as e:
#print(proxy "不可用") pass
getip.py
import scrapy from proxy.items import ProxyItem import re class GetipSpider(scrapy.Spider):
name = 'getip' allowed_domains = ['xicidaili.com']
start_urls = [ 'http://www.xicidaili.com/wn/', 'http://www.xicidaili.com/wt/', ] def parse(self, response):
item = ProxyItem()
item["IP"] = response.xpath("//tr[@class = 'odd']/td[2]/text()").extract()
item["port"] = response.xpath("//tr[@class = 'odd']/td[3]/text()").extract()
item["nmd"] = response.xpath("//tr[@class = 'odd']/td[5]/text()").extract()
item["type"] = response.xpath("//tr[@class = 'odd']/td[6]/text()").extract()
item["addr"] = response.xpath("//tr[@class = 'odd']/td[4]/a/text()").extract()
item["lastime"] = response.xpath("//tr[@class = 'odd']/td[10]/text()").extract() #print(item) yield item #rang = response.xpath("//div[@id = 'listnav']/ul/li[6]/a/text()").extract() #下一页,前5页最后验证时间临近的IP #print(int(rang[0]) 1) type = response.xpath("//div[@class='pagination']/a[last()]/@href").extract()[0]
type = re.findall(r'/(.*?)/',type,re.S) #print(type) for i in range(2,4):
next_page = response.urljoin('http://www.xicidaili.com/%s/%s/') % (type[0],i) print(next_page) #next_page = response.urljoin('https://www.kuaidaili.com%s%s/') % type % i yield scrapy.Request(next_page, callback=self.parse)
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论