欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Scrapy框架爬取西刺代理網(wǎng)免費(fèi)高匿代理的實(shí)現(xiàn)代碼

 更新時(shí)間:2019年02月22日 10:14:54   作者:Money多多  
今天小編就為大家分享一篇關(guān)于Scrapy框架爬取西刺代理網(wǎng)免費(fèi)高匿代理的實(shí)現(xiàn)代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

分析

需求:

爬取西刺代理網(wǎng)免費(fèi)高匿代理,并保存到MySQL數(shù)據(jù)庫(kù)中。

這里只爬取前10頁(yè)中的數(shù)據(jù)。

思路:

  1. 分析網(wǎng)頁(yè)結(jié)構(gòu),確定數(shù)據(jù)提取規(guī)則
  2. 創(chuàng)建Scrapy項(xiàng)目
  3. 編寫(xiě)item,定義數(shù)據(jù)字段
  4. 編寫(xiě)spider,實(shí)現(xiàn)數(shù)據(jù)抓取
  5. 編寫(xiě)Pipeline,保存數(shù)據(jù)到數(shù)據(jù)庫(kù)中
  6. 配置settings.py文件
  7. 運(yùn)行爬蟲(chóng)項(xiàng)目

代碼實(shí)現(xiàn)

items.py

import scrapy
class XicidailiItem(scrapy.Item):
  # 國(guó)家
  country=scrapy.Field()
  # IP地址
  ip=scrapy.Field()
  # 端口號(hào)
  port=scrapy.Field()
  # 服務(wù)器地址
  address=scrapy.Field()
  # 是否匿名
  anonymous=scrapy.Field()
  # 類(lèi)型
  type=scrapy.Field()
  # 速度
  speed=scrapy.Field()
  # 連接時(shí)間
  connect_time=scrapy.Field()
  # 存活時(shí)間
  alive_time=scrapy.Field()
  # 驗(yàn)證時(shí)間
  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:]: # 過(guò)濾掉表頭行
      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):
  """
  西刺代理爬蟲(chóng) 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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • 對(duì)Python3中的input函數(shù)詳解

    對(duì)Python3中的input函數(shù)詳解

    下面小編就為大家分享一篇對(duì)Python3中的input函數(shù)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python實(shí)現(xiàn)登錄與注冊(cè)功能

    python實(shí)現(xiàn)登錄與注冊(cè)功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)登錄與注冊(cè)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Django配置kafka消息隊(duì)列的實(shí)現(xiàn)

    Django配置kafka消息隊(duì)列的實(shí)現(xiàn)

    本文主要介紹了Django配置kafka消息隊(duì)列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Python 實(shí)現(xiàn)取矩陣的部分列,保存為一個(gè)新的矩陣方法

    Python 實(shí)現(xiàn)取矩陣的部分列,保存為一個(gè)新的矩陣方法

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)取矩陣的部分列,保存為一個(gè)新的矩陣方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • python去除列表中的空值元素實(shí)戰(zhàn)技巧

    python去除列表中的空值元素實(shí)戰(zhàn)技巧

    這篇文章主要介紹了python實(shí)戰(zhàn)技巧之去除列表中的空值元素,搜集針對(duì)python高效處理數(shù)據(jù)的核心代碼,今天是實(shí)現(xiàn)去除列表中的空值元素,需要的朋友可以參考下
    2023-02-02
  • Python中用字符串調(diào)用函數(shù)或方法示例代碼

    Python中用字符串調(diào)用函數(shù)或方法示例代碼

    字符串作為python中常用的數(shù)據(jù)類(lèi)型,掌握字符串的常用方法十分必要。下面這篇文章主要給大家介紹了關(guān)于Python中通過(guò)字符串調(diào)用函數(shù)或方法的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • python循環(huán)定時(shí)中斷執(zhí)行某一段程序的實(shí)例

    python循環(huán)定時(shí)中斷執(zhí)行某一段程序的實(shí)例

    今天小編就為大家分享一篇python循環(huán)定時(shí)中斷執(zhí)行某一段程序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python中用sleep()方法操作時(shí)間的教程

    Python中用sleep()方法操作時(shí)間的教程

    這篇文章主要介紹了Python中用sleep()方法操作時(shí)間的教程,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • 你知道嗎實(shí)現(xiàn)炫酷可視化只要1行python代碼

    你知道嗎實(shí)現(xiàn)炫酷可視化只要1行python代碼

    這篇文章主要給大家介紹了關(guān)于利用Python進(jìn)行數(shù)據(jù)可視化常見(jiàn)的9種方法!文中介紹的方法真的超實(shí)用!對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • python打印帶時(shí)間的日志實(shí)現(xiàn)代碼

    python打印帶時(shí)間的日志實(shí)現(xiàn)代碼

    python的logging模塊提供了標(biāo)準(zhǔn)的日志接口,可以通過(guò)它存儲(chǔ)各種格式的日志,下面這篇文章主要給大家介紹了關(guān)于python打印帶時(shí)間的日志的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04

最新評(píng)論