Scrapy框架爬取西刺代理網(wǎng)免費(fèi)高匿代理的實(shí)現(xiàn)代碼
分析
需求:
爬取西刺代理網(wǎng)免費(fèi)高匿代理,并保存到MySQL數(shù)據(jù)庫中。
這里只爬取前10頁中的數(shù)據(jù)。

思路:
- 分析網(wǎng)頁結(jié)構(gòu),確定數(shù)據(jù)提取規(guī)則
- 創(chuàng)建Scrapy項(xiàng)目
- 編寫item,定義數(shù)據(jù)字段
- 編寫spider,實(shí)現(xiàn)數(shù)據(jù)抓取
- 編寫Pipeline,保存數(shù)據(jù)到數(shù)據(jù)庫中
- 配置settings.py文件
- 運(yùn)行爬蟲項(xiàng)目
代碼實(shí)現(xiàn)
items.py
import scrapy class XicidailiItem(scrapy.Item): # 國家 country=scrapy.Field() # IP地址 ip=scrapy.Field() # 端口號 port=scrapy.Field() # 服務(wù)器地址 address=scrapy.Field() # 是否匿名 anonymous=scrapy.Field() # 類型 type=scrapy.Field() # 速度 speed=scrapy.Field() # 連接時間 connect_time=scrapy.Field() # 存活時間 alive_time=scrapy.Field() # 驗(yàn)證時間 verify_time=scrapy.Field()
xicidaili_spider.py
# !/usr/bin/env python
# -*- coding:utf-8 -*-
import scrapy
from myscrapy.items import XicidailiItem
class XicidailiSpider(scrapy.Spider):
name = 'xicidaili'
allowed_domains=['www.xicidaili.com']
# start_urls=['http://www.xicidaili.com/nn/1']
def start_requests(self):
urls=[]
for i in range(1,11):
urls.append('http://www.xicidaili.com/nn/'+str(i))
for url in urls:
yield scrapy.Request(url,callback=self.parse,method='GET')
def parse(self, response):
tr_list=response.xpath('//table[@id="ip_list"]/tr')
for tr in tr_list[1:]: # 過濾掉表頭行
item=XicidailiItem()
item['country']=tr.xpath('./td[1]/img/@alt').extract_first()
item['ip']=tr.xpath('./td[2]/text()').extract_first()
item['port']=tr.xpath('./td[3]/text()').extract_first()
item['address']=tr.xpath('./td[4]/a/text()').extract_first()
item['anonymous']=tr.xpath('./td[5]/text()').extract_first()
item['type']=tr.xpath('./td[6]/text()').extract_first()
item['speed']=tr.xpath('./td[7]/div/@title').re(r'\d{1,3}\.\d{0,}')[0]
item['connect_time']=tr.xpath('./td[8]/div/@title').re(r'\d{1,3}\.\d{0,}')[0]
item['alive_time']=tr.xpath('./td[9]/text()').extract_first()
item['verify_time']=tr.xpath('./td[10]/text()').extract_first()
yield item
pipelines.py
class XicidailiPipeline(object):
"""
西刺代理爬蟲 item Pipeline
create table xicidaili(
id int primary key auto_increment,
country varchar(10) not null,
ip varchar(30) not null,
port varchar(10) not null,
address varchar(30) not null,
anonymous varchar(10) not null,
type varchar(20) not null,
speed varchar(10) not null,
connect_time varchar(20) not null,
alive_time varchar(20) not null,
verify_time varchar(20) not null);
"""
def __init__(self):
self.connection = pymysql.connect(host='localhost',
user='root',
password='123456',
db='mydb',
charset='utf8', # 不能用utf-8
cursorclass=pymysql.cursors.DictCursor)
def process_item(self,item,spider):
with self.connection.cursor() as cursor:
sql='insert into xicidaili' \
'(country,ip,port,address,anonymous,type,speed,connect_time,alive_time,verify_time) values' \
'(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);'
args=(item['country'],item['ip'],item['port'],item['address'],item['anonymous'],item['type'],item['speed'],item['connect_time'],item['alive_time'],item['verify_time'])
spider.logger.info(args)
cursor.execute(sql,args)
self.connection.commit()
def close_spider(self,spider):
self.connection.close()
settings.py
ITEM_PIPELINES = {
'myscrapy.pipelines.XicidailiPipeline': 300,
}
結(jié)果


總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
python?opencv圖像的高通濾波和低通濾波的示例代碼
這篇文章主要介紹了python?opencv圖像的高通濾波和低通濾波,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
Python何時應(yīng)該使用Lambda函數(shù)
這篇文章主要介紹了Python何時應(yīng)該使用Lambda函數(shù),Python 中定義函數(shù)有兩種方法,一種是用常規(guī)方式 def 定義,函數(shù)要指定名字,第二種是用 lambda 定義,不需要指定名字,稱為 Lambda 函數(shù),需要的朋友可以參考下2019-07-07
10行Python代碼就能實(shí)現(xiàn)的八種有趣功能詳解
Python憑借其簡潔的代碼,贏得了許多開發(fā)者的喜愛,因此也就促使了更多開發(fā)者用Python開發(fā)新的模塊。面我們來看看,我們用不超過10行代碼能實(shí)現(xiàn)些什么有趣的功能吧2022-03-03
Python讀取配置文件-ConfigParser的二次封裝方法
這篇文章主要介紹了Python讀取配置文件-ConfigParser的二次封裝方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
python數(shù)字圖像處理之圖像簡單濾波實(shí)現(xiàn)
這篇文章主要為大家介紹了python數(shù)字圖像處理之圖像簡單濾波實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
用python實(shí)現(xiàn)一個簡單的驗(yàn)證碼
這篇文章主要介紹了用python實(shí)現(xiàn)一個簡單的驗(yàn)證碼的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12

