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

詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫(kù)

 更新時(shí)間:2021年01月24日 17:08:13   作者:我不是禿頭哆唻咪  
這篇文章主要介紹了詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

獲取要爬取的URL

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

爬蟲前期工作

在這里插入圖片描述

用Pycharm打開項(xiàng)目開始寫爬蟲文件

字段文件items

# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy


class NbaprojectItem(scrapy.Item):
  # define the fields for your item here like:
  # name = scrapy.Field()
  # pass
  # 創(chuàng)建字段的固定格式-->scrapy.Field()
  # 英文名
  engName = scrapy.Field()
  # 中文名
  chName = scrapy.Field()
  # 身高
  height = scrapy.Field()
  # 體重
  weight = scrapy.Field()
  # 國(guó)家英文名
  contryEn = scrapy.Field()
  # 國(guó)家中文名
  contryCh = scrapy.Field()
  # NBA球齡
  experience = scrapy.Field()
  # 球衣號(hào)碼
  jerseyNo = scrapy.Field()
  # 入選年
  draftYear = scrapy.Field()
  # 隊(duì)伍英文名
  engTeam = scrapy.Field()
  # 隊(duì)伍中文名
  chTeam = scrapy.Field()
  # 位置
  position = scrapy.Field()
  # 東南部
  displayConference = scrapy.Field()
  # 分區(qū)
  division = scrapy.Field()

爬蟲文件

import scrapy
import json
from nbaProject.items import NbaprojectItem

class NbaspiderSpider(scrapy.Spider):
  name = 'nbaSpider'
  allowed_domains = ['nba.com']
  # 第一次爬取的網(wǎng)址,可以寫多個(gè)網(wǎng)址
  # start_urls = ['http://nba.com/']
  start_urls = ['https://china.nba.com/static/data/league/playerlist.json']
  # 處理網(wǎng)址的response
  def parse(self, response):
    # 因?yàn)樵L問的網(wǎng)站返回的是json格式,首先用第三方包處理json數(shù)據(jù)
    data = json.loads(response.text)['payload']['players']
    # 以下列表用來存放不同的字段
    # 英文名
    engName = []
    # 中文名
    chName = []
    # 身高
    height = []
    # 體重
    weight = []
    # 國(guó)家英文名
    contryEn = []
    # 國(guó)家中文名
    contryCh = []
    # NBA球齡
    experience = []
    # 球衣號(hào)碼
    jerseyNo = []
    # 入選年
    draftYear = []
    # 隊(duì)伍英文名
    engTeam = []
    # 隊(duì)伍中文名
    chTeam = []
    # 位置
    position = []
    # 東南部
    displayConference = []
    # 分區(qū)
    division = []
    # 計(jì)數(shù)
    count = 1
    for i in data:
      # 英文名
      engName.append(str(i['playerProfile']['firstNameEn'] + i['playerProfile']['lastNameEn']))
      # 中文名
      chName.append(str(i['playerProfile']['firstName'] + i['playerProfile']['lastName']))
      # 國(guó)家英文名
      contryEn.append(str(i['playerProfile']['countryEn']))
      # 國(guó)家中文
      contryCh.append(str(i['playerProfile']['country']))
      # 身高
      height.append(str(i['playerProfile']['height']))
      # 體重
      weight.append(str(i['playerProfile']['weight']))
      # NBA球齡
      experience.append(str(i['playerProfile']['experience']))
      # 球衣號(hào)碼
      jerseyNo.append(str(i['playerProfile']['jerseyNo']))
      # 入選年
      draftYear.append(str(i['playerProfile']['draftYear']))
      # 隊(duì)伍英文名
      engTeam.append(str(i['teamProfile']['code']))
      # 隊(duì)伍中文名
      chTeam.append(str(i['teamProfile']['displayAbbr']))
      # 位置
      position.append(str(i['playerProfile']['position']))
      # 東南部
      displayConference.append(str(i['teamProfile']['displayConference']))
      # 分區(qū)
      division.append(str(i['teamProfile']['division']))

      # 創(chuàng)建item字段對(duì)象,用來存儲(chǔ)信息 這里的item就是對(duì)應(yīng)上面導(dǎo)的NbaprojectItem
      item = NbaprojectItem()
      item['engName'] = str(i['playerProfile']['firstNameEn'] + i['playerProfile']['lastNameEn'])
      item['chName'] = str(i['playerProfile']['firstName'] + i['playerProfile']['lastName'])
      item['contryEn'] = str(i['playerProfile']['countryEn'])
      item['contryCh'] = str(i['playerProfile']['country'])
      item['height'] = str(i['playerProfile']['height'])
      item['weight'] = str(i['playerProfile']['weight'])
      item['experience'] = str(i['playerProfile']['experience'])
      item['jerseyNo'] = str(i['playerProfile']['jerseyNo'])
      item['draftYear'] = str(i['playerProfile']['draftYear'])
      item['engTeam'] = str(i['teamProfile']['code'])
      item['chTeam'] = str(i['teamProfile']['displayAbbr'])
      item['position'] = str(i['playerProfile']['position'])
      item['displayConference'] = str(i['teamProfile']['displayConference'])
      item['division'] = str(i['teamProfile']['division'])
      # 打印爬取信息
      print("傳輸了",count,"條字段")
      count += 1
      # 將字段交回給引擎 -> 管道文件
      yield item

配置文件->開啟管道文件

在這里插入圖片描述

在這里插入圖片描述

# Scrapy settings for nbaProject project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#   https://docs.scrapy.org/en/latest/topics/settings.html
#   https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#   https://docs.scrapy.org/en/latest/topics/spider-middleware.html
# ----------不做修改部分---------
BOT_NAME = 'nbaProject'

SPIDER_MODULES = ['nbaProject.spiders']
NEWSPIDER_MODULE = 'nbaProject.spiders'
# ----------不做修改部分---------

# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'nbaProject (+http://www.yourdomain.com)'

# Obey robots.txt rules
# ----------修改部分(可以自行查這是啥東西)---------
# ROBOTSTXT_OBEY = True
# ----------修改部分---------

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://docs.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#  'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See https://docs.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#  'nbaProject.middlewares.NbaprojectSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#  'nbaProject.middlewares.NbaprojectDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://docs.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#  'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# 開啟管道文件
# ----------修改部分---------
ITEM_PIPELINES = {
  'nbaProject.pipelines.NbaprojectPipeline': 300,
}
# ----------修改部分---------
# Enable and configure the AutoThrottle extension (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://docs.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

管道文件 -> 將字段寫進(jìn)mysql

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pymysql
class NbaprojectPipeline:
	# 初始化函數(shù)
  def __init__(self):
    # 連接數(shù)據(jù)庫(kù) 注意修改數(shù)據(jù)庫(kù)信息
    self.connect = pymysql.connect(host='域名', user='用戶名', passwd='密碼',
                    db='數(shù)據(jù)庫(kù)', port=端口號(hào)) 
    # 獲取游標(biāo)
    self.cursor = self.connect.cursor()
    # 創(chuàng)建一個(gè)表用于存放item字段的數(shù)據(jù)
    createTableSql = """
              create table if not exists `nbaPlayer`(
              playerId INT UNSIGNED AUTO_INCREMENT,
              engName varchar(80),
              chName varchar(20),
              height varchar(20),
              weight varchar(20),
              contryEn varchar(50),
              contryCh varchar(20),
              experience int,
              jerseyNo int,
              draftYear int,
              engTeam varchar(50),
              chTeam varchar(50),
              position varchar(50),
              displayConference varchar(50),
              division varchar(50),
              primary key(playerId)
              )charset=utf8;
              """
    # 執(zhí)行sql語句
    self.cursor.execute(createTableSql)
    self.connect.commit()
    print("完成了創(chuàng)建表的工作")
	#每次yield回來的字段會(huì)在這里做處理
  def process_item(self, item, spider):
  	# 打印item增加觀賞性
  	print(item)
    # sql語句
    insert_sql = """
    insert into nbaPlayer(
    playerId, engName, 
    chName,height,
    weight,contryEn,
    contryCh,experience,
    jerseyNo,draftYear
    ,engTeam,chTeam,
    position,displayConference,
    division
    ) VALUES (null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
    """
    # 執(zhí)行插入數(shù)據(jù)到數(shù)據(jù)庫(kù)操作
    # 參數(shù)(sql語句,用item字段里的內(nèi)容替換sql語句的占位符)
    self.cursor.execute(insert_sql, (item['engName'], item['chName'], item['height'], item['weight']
                     , item['contryEn'], item['contryCh'], item['experience'], item['jerseyNo'],
                     item['draftYear'], item['engTeam'], item['chTeam'], item['position'],
                     item['displayConference'], item['division']))
    # 提交,不進(jìn)行提交無法保存到數(shù)據(jù)庫(kù)
    self.connect.commit()
    print("數(shù)據(jù)提交成功!")

啟動(dòng)爬蟲

在這里插入圖片描述

屏幕上滾動(dòng)的數(shù)據(jù)

在這里插入圖片描述

去數(shù)據(jù)庫(kù)查看數(shù)據(jù)

在這里插入圖片描述

簡(jiǎn)簡(jiǎn)單單就把球員數(shù)據(jù)爬回來啦~

到此這篇關(guān)于詳解Python之Scrapy爬蟲教程N(yùn)BA球員數(shù)據(jù)存放到Mysql數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)Scrapy爬蟲員數(shù)據(jù)存放到Mysql內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中

    如何使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中

    wxPython 提供了一個(gè)簡(jiǎn)單易用的界面,而 FFmpeg 則負(fù)責(zé)處理視頻幀的提取,這個(gè)工具不僅對(duì)視頻編輯工作有幫助,也為批量處理視頻文件提供了極大的便利,這篇文章主要介紹了使用 Python和 FFmpeg 批量截圖視頻到各自文件夾中,需要的朋友可以參考下
    2024-08-08
  • Python中的pathlib.Path為什么不繼承str詳解

    Python中的pathlib.Path為什么不繼承str詳解

    這篇文章主要給大家介紹了關(guān)于Python中pathlib.Path為什么不繼承str的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • python如何實(shí)現(xiàn)MK突變檢驗(yàn)方法,代碼復(fù)制修改可用

    python如何實(shí)現(xiàn)MK突變檢驗(yàn)方法,代碼復(fù)制修改可用

    這篇文章主要介紹了python如何實(shí)現(xiàn)MK突變檢驗(yàn)方法,代碼復(fù)制修改可用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python中元組解構(gòu)的技巧詳解

    Python中元組解構(gòu)的技巧詳解

    在Python中,元組(tuple)是一種常用的數(shù)據(jù)結(jié)構(gòu),元組的解構(gòu)是一項(xiàng)強(qiáng)大的特性,快速、方便地將元組中的值分配給多個(gè)變量,下面我們就來學(xué)習(xí)一下Python中元組解構(gòu)的技巧吧
    2024-01-01
  • Python實(shí)戰(zhàn)練習(xí)之終于對(duì)肯德基下手

    Python實(shí)戰(zhàn)練習(xí)之終于對(duì)肯德基下手

    讀萬卷書不如行萬里路,學(xué)的扎不扎實(shí)要通過實(shí)戰(zhàn)才能看出來,本篇文章手把手帶你爬下肯德基的官網(wǎng),大家可以在過程中查缺補(bǔ)漏,看看自己掌握程度怎么樣
    2021-10-10
  • Python使用修飾器進(jìn)行異常日志記錄操作示例

    Python使用修飾器進(jìn)行異常日志記錄操作示例

    這篇文章主要介紹了Python使用修飾器進(jìn)行異常日志記錄操作,結(jié)合實(shí)例形式分析了Python基于修飾器的log日志文件操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • python 實(shí)現(xiàn)的IP 存活掃描腳本

    python 實(shí)現(xiàn)的IP 存活掃描腳本

    這篇文章主要介紹了python 實(shí)現(xiàn)的IP 存活掃描腳本,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • 深入了解Python如何操作MongoDB

    深入了解Python如何操作MongoDB

    MongoDB是由C++語言編寫的非關(guān)系型數(shù)據(jù)庫(kù),是一個(gè)基于分布式文件存儲(chǔ)的開源數(shù)據(jù)庫(kù)系統(tǒng)。本文主要介紹了如何通過Python操作MongoDB,需要的可以參考一下
    2022-01-01
  • Pytorch訓(xùn)練網(wǎng)絡(luò)過程中l(wèi)oss突然變?yōu)?的解決方案

    Pytorch訓(xùn)練網(wǎng)絡(luò)過程中l(wèi)oss突然變?yōu)?的解決方案

    這篇文章主要介紹了Pytorch訓(xùn)練網(wǎng)絡(luò)過程中l(wèi)oss突然變?yōu)?的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • Python做個(gè)自定義動(dòng)態(tài)壁紙還可以放視頻

    Python做個(gè)自定義動(dòng)態(tài)壁紙還可以放視頻

    這篇文章主要介紹了如何用Python做個(gè)可以放視頻自定義動(dòng)態(tài)壁紙,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08

最新評(píng)論