您的位置:首页 > 数据库 > MySQL

对"西刺免费代理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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  scrapy IP代理