对"西刺免费代理IP"爬取、测试并存入MySQL
2018-02-23 16:44
253 查看
前言
学习scrapy有一段时间了,但是对了笔记的总结并没有跟上步伐,这个案例是视频教程多次给出的,但是在此进行总结和学习,提高学习效率。由于网站结构发生改变,这篇文章的代码也随之发生改变。
Python网络爬虫实战 Scrapy
注:b站真是个好地方。
思路
对了需求无非进行下面的顺序操作。爬取IP信息
验证IP信息
存储IP信息
爬取
新建项目scrapy startproject collectips
进入项目
cd collectips
建立模板
scrapy genspider xici xicidaili.com
分析结构(个人习惯)
scrapy shell http://www.xicidaili.com/nn/ #分析要存取的数据(此处注意表格的提取方式)
注:结构发现无论如何编写shell命令,返回内容都为空。有幸在CSDN的一篇博文告诉了我答案。 Scrapy抓取西刺高匿代理ip
更改命令
scrapy shell -s USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36" "http://www.xicidaili.com/nn/"
注:除命令改变,编写爬虫的设置文件Settings.py也需要改变。
设置User-agent
更改配置文件中USER-AGENT一行,将注释去掉。可以将其设置为
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
结构分析
xpath
ip = response.xpath('//table[@id="ip_list"]/tr/td[3]/text()').extract() port = response.xpath('//table[@id="ip_list"]/tr/td[3]/text()').extract() address = response.xpath('//table[@id="ip_list"]/tr/td[4]/a/text()').extract() annoy = response.xpath('//table[@id="ip_list"]/tr/td[5]/text()').extract() type = response.xpath('//table[@id="ip_list"]/tr/td[6]/text()').extract() speed = response.xpath('//table[@id="ip_list"]/tr/td[7]/div/@title').re('\d{0,2}\.\d{0,}') time = response.xpath('//table[@id="ip_list"]/tr/td[8]/div/@title').re('\d{0,2}\.\d{0,}') live = response.xpath('//table[@id="ip_list"]/tr/td[9]/text()').extract() check = response.xpath('//table[@id="ip_list"]/tr/td[10]/text()').extract()
css
ip = item.css("td:nth-child(2)::text").extract() port = item.css("td:nth-child(3)::text").extract() address = item.css("td:nth-child(4) a::text").extract() type = item.css("td:nth-child(5)::text").extract() protocol = item.css("td:nth-child(6)::text").extract() speed = item.css("td:nth-child(7) div::attr(title)").extract() time = item.css("td:nth-child(8) div::attr(title)").extract() alive = item.css("td:nth-child(9)::text").extract() proof = item.css("td:nth-child(10)::text").extract()
编辑item.py
# -*- coding: utf-8 -*- # Define here the models for your scraped items # # See documentation in: # https://doc.scrapy.org/en/latest/topics/items.html import scrapy class CollectipsItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() ip = scrapy.Field() port = scrapy.Field() address = scrapy.Field() annoy = scrapy.Field() type = scrapy.Field() speed = scrapy.Field() time = scrapy.Field() live = scrapy.Field() check = scrapy.Field()
更改xici.py
# -*- coding: utf-8 -*- import scrapy from collectips.items import CollectipsItem class XiciSpider(scrapy.Spider): name = 'xici' allowed_domains = ['xicidaili.com'] start_urls = ['http://www.xicidaili.com'] #开始请求地址 def start_requests(self): reqs = [] for i in range(1,2600): req=scrapy.Request("http://www.xicidaili.com/nn/%s"%i) reqs.append(req) return reqs b3cc def parse(self, response): item = [] for info in response.xpath('//table[@id="ip_list"]/tr')[1:]: collecte = CollectipsItem() collecte['ip'] = info.xpath('td[3]/text()').extract_first() collecte['port'] = info.xpath('td[3]/text()').extract_first() collecte['address'] = info.xpath('td[4]/a/text()').extract_first() collecte['annoy'] = info.xpath('td[5]/text()').extract_first() collecte['type'] = info.xpath('td[6]/text()').extract_first() collecte['speed'] = info.xpath('td[7]/div/@title').re('\d{0,2}\.\d{0,}') collecte['time'] = info.xpath('td[8]/div/@title').re('\d{0,2}\.\d{0,}') collecte['live'] = info.xpath('td[9]/text()').extract_first() collecte['check'] = info.xpath('td[10]/text()').extract_first() item.append(collecte) return item
运行爬虫
scrapy crawl xici -o xici.json
结果日志文件返回大量
503 Service Unavailable,用浏览器也无法访问,看来代理访问尤其必要。待恢复后进行缓慢爬取IP。
相关文章推荐
- 对"瑶瑶代理IP"的爬取存取MySQL
- 通过"named pipe"方式或"TCP/IP"方式,测试本机MySQL客户端对本机MySQL服务器的连接通信
- SocksHub.net 全球百万级海量ip代理,代理服务器免费测试,认准唯一联系方式,谨防上当受骗。
- 无忧代理免费ip爬取(端口js加密)
- 蚂蚁代理免费代理ip爬取(端口图片显示+token检查)
- 免费3个月的VPS送独立IP (经测试可以终身免费)
- Python中抓取代理IP并测试
- python3获取免费代理IP
- 使用免费ip代理进行投票
- 第四章 爬取西刺免费代理ip 并应用到scrapy
- 2016"百度之星" - 测试赛(热身,陈题)&& 列变位法解密&&IP聚合&&放盘子
- python 爬取西刺免费代理ip 并使用telnetlib.Telnet验证是否有效
- 使用免费ip代理进行投票
- master_ip_failover 测试可以使用的IP 地址切换脚本 --for Mysql-Master-Ha
- 免费的国内代理服务器、国内代理IP地址
- 使用gecco获取代理IP,仅测试代码用,不要用于非法用途
- IP聚合 (2016"百度之星" - 测试赛(热身,陈题))
- 免费的HTTP代理IP服务器地址
- 大共享免费云服务器 Linux Nginx php mysql phpwind 性能测试
- 测试ip是否可用(ip代理时)