内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

如何利用scrapy將爬到的數(shù)據(jù)保存到mysql-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“如何利用scrapy將爬到的數(shù)據(jù)保存到mysql”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何利用scrapy將爬到的數(shù)據(jù)保存到mysql”這篇文章吧。

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設、域名、虛擬空間、網(wǎng)站運營、企業(yè)網(wǎng)站設計、宣城網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

1.環(huán)境建立

     1.使用xmapp安裝php, mysql ,phpmyadmin

     2.安裝python3,pip

     3.安裝pymysql

     3.(windows 略)我這邊是mac,安裝brew,用brew 安裝scrapy

2.整個流程

     1. 創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)庫表,準備保存

     2.寫入爬蟲目標URL,進行網(wǎng)絡請求

     3.對爬返回數(shù)據(jù)進行處理,得到具體數(shù)據(jù)

     4.對于具體數(shù)據(jù)保存到數(shù)據(jù)庫中

2.1.創(chuàng)建數(shù)據(jù)庫

首先創(chuàng)建一個數(shù)據(jù)庫叫scrapy,然后創(chuàng)建一個表article,我們這里給body加了唯一索引,防止重復插入數(shù)據(jù)

--
-- Database: `scrapy`
--
 
-- --------------------------------------------------------
 
--
-- 表的結(jié)構(gòu) `article`
--
 
CREATE TABLE `article` (
 `id` int(11) NOT NULL,
 `body` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
 `author` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
 `createDate` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 
--
-- Indexes for table `article`
--
ALTER TABLE `article`
 ADD PRIMARY KEY (`id`),
 ADD UNIQUE KEY `uk_body` (`body`);

如何利用scrapy將爬到的數(shù)據(jù)保存到mysql

弄好以后是這樣的。

2.2 先看下整個爬蟲項目的結(jié)構(gòu)

如何利用scrapy將爬到的數(shù)據(jù)保存到mysql

quotes_spider.py是核心,負責對網(wǎng)絡請求和對內(nèi)容進行處理,然后對整理好的內(nèi)容拋給pipelines進行具體處理,保存到數(shù)據(jù)庫中,這樣不會影響速度。

其他的看 圖說明

2.2 寫入爬蟲目標URL,進行網(wǎng)絡請求

import scrapy
from tutorial.items import TutorialItem
class QuotesSpider(scrapy.Spider):
 name = "quotes"
 def start_requests(self):
  url = 'http://quotes.toscrape.com/tag/humor/'
  yield scrapy.Request(url)
 def parse(self, response):
  item = TutorialItem()
  for quote in response.css('div.quote'):
   item['body'] = quote.css('span.text::text').extract_first()
   item['author'] = quote.css('small.author::text').extract_first()
   yield item
  next_page = response.css('li.next a::attr("href")').extract_first()
  if next_page is not None:
   yield response.follow(next_page, self.parse)

start_requests 就是要寫入具體要爬的URL

parse就是核心的對返回的數(shù)據(jù)進行處理的地方,然后以item的形式拋出,接下來定義好下一個要爬的內(nèi)容 

2.3  items

# -*- 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 TutorialItem(scrapy.Item):
 body = scrapy.Field()
 author = scrapy.Field()
 pass

2.4 pipelines

# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
import datetime
from tutorial import settings
import logging
class TutorialPipeline(object):
 def __init__(self):
  self.connect = pymysql.connect(
   host = settings.MYSQL_HOST,
   db = settings.MYSQL_DBNAME,
   user = settings.MYSQL_USER,
   passwd = settings.MYSQL_PASSWD,
   charset = 'utf8',
   use_unicode = True
  )
  self.cursor = self.connect.cursor(); 
 def process_item(self, item, spider):
  try:
   self.cursor.execute(
    "insert into article (body, author, createDate) value(%s, %s, %s) on duplicate key update author=(author)",
    (item['body'],
     item['author'],
     datetime.datetime.now()
     ))
   self.connect.commit()
  except Exception as error:
   logging.log(error)
  return item
 def close_spider(self, spider):
  self.connect.close();

2.5 配置

ITEM_PIPELINES = {
 'tutorial.pipelines.TutorialPipeline':300
}
MYSQL_HOST = 'localhost'
MYSQL_DBNAME = 'scrapy'
MYSQL_USER = 'root'
MYSQL_PASSWD = '123456'
MYSQL_PORT = 3306

3.啟動爬蟲

scrapy crawl quotes

以上是“如何利用scrapy將爬到的數(shù)據(jù)保存到mysql”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:如何利用scrapy將爬到的數(shù)據(jù)保存到mysql-創(chuàng)新互聯(lián)
分享鏈接:http://www.rwnh.cn/article38/cssesp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、網(wǎng)站維護、電子商務、自適應網(wǎng)站網(wǎng)站排名、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司
广元市| 渝中区| 怀宁县| 永丰县| 永年县| 青海省| 松江区| 安西县| 阳曲县| 崇阳县| 海晏县| 阿尔山市| 彩票| 肥东县| 托克逊县| 峨边| 齐齐哈尔市| 海口市| 黄龙县| 绥宁县| 阿克| 岳西县| 汉阴县| 衡南县| 乌审旗| 英超| 托克托县| 上蔡县| 绥中县| 呼玛县| 兰溪市| 长乐市| 博罗县| 陆丰市| 甘德县| 益阳市| 循化| 崇左市| 望江县| 黔西县| 泾源县|