欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用Scrapy怎么對動態(tài)數(shù)據(jù)進(jìn)行爬取-創(chuàng)新互聯(lián)

使用Scrapy怎么對動態(tài)數(shù)據(jù)進(jìn)行爬取?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

我們提供的服務(wù)有:網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、阿克蘇ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的阿克蘇網(wǎng)站制作公司

1.首先第一步,對中間件的設(shè)置。

進(jìn)入pipelines.py文件中:

from selenium import webdriver
from scrapy.http.response.html import HtmlResponse
from scrapy.http.response import Response
class SeleniumSpiderMiddleware(object):
  def __init__(self):
    self.driver = webdriver.PhantomJS()
  def process_request(self ,request ,spider):
    # 當(dāng)引擎從調(diào)度器中取出request進(jìn)行請求發(fā)送下載器之前
    # 會先執(zhí)行當(dāng)前的爬蟲中間件 ,在中間件里面使用selenium
    # 請求這個request ,拿到動態(tài)網(wǎng)站的數(shù)據(jù) 然后將請求
    # 返回給spider爬蟲對象
    if spider.name == 'taobao':
      # 使用爬蟲文件的url地址
      spider.driver.get(request.url)
      for x in range(1 ,12 ,2):
        i = float(x) / 11
        # scrollTop 從上往下的滑動距離
        js = 'document.body.scrollTop=document.body.scrollHeight * %f' % i
        spider.driver.execute_script(js)
      response = HtmlResponse(url=request.url,
                  body=spider.driver.page_source,
                  encoding='utf-8',
                  request=request)
      # 這個地方只能返回response對象,當(dāng)返回了response對象,那么可以直接跳過下載中間件,將response的值傳遞給引擎,引擎又傳遞給 spider進(jìn)行解析
      return response

在設(shè)置中,要將middlewares設(shè)置打開。

進(jìn)入settings.py文件中,將

DOWNLOADER_MIDDLEWARES = {
  'taobaoSpider.middlewares.SeleniumSpiderMiddleware': 543,
}

打開。

2.第二步,爬取數(shù)據(jù)

回到spider爬蟲文件中。

引入:

from selenium import webdriver

自定義屬性:

def __init__(self):
  self.driver = webdriver.PhantomJS()

查找數(shù)據(jù)和分析數(shù)據(jù):

def parse(self, response):
  div_info = response.xpath('//div[@class="info-cont"]')
  print(div_info)
  for div in div_info:
    title = div.xpath('.//div[@class="title-row "]/a/text()').extract_first('')
    # title = self.driver.find_element_by_class_name("title-row").text
    print('名稱:', title)
    price = div.xpath('.//div[@class="sale-row row"]/div/span[2]/strong/text()').extract_first('')

3.第三步,傳送數(shù)據(jù)到item中:

item.py文件中:

name = scrapy.Field()
price = scrapy.Field()

回到spider.py爬蟲文件中:

引入:

from ..items import TaobaospiderItem

傳送數(shù)據(jù):

#創(chuàng)建實例化對象。

item = TaobaospiderItem()
item['name'] = title
item['price'] = price
yield item

在設(shè)置中,打開:

ITEM_PIPELINES = {
  'taobaoSpider.pipelines.TaobaospiderPipeline': 300,
}

4.第四步,寫入數(shù)據(jù)庫:

進(jìn)入管道文件中。

引入

import sqlite3
寫入數(shù)據(jù)庫的代碼如下:
class TaobaospiderPipeline(object):
  def __init__(self):
    self.connect = sqlite3.connect('taobaoDB')
    self.cursor = self.connect.cursor()
    self.cursor.execute('create table if not exists taobaoTable (name text,price text)')
  def process_item(self, item, spider):
    self.cursor.execute('insert into taobaoTable (name,price)VALUES ("{}","{}")'.format(item['name'],item['price']))
    self.connect.commit()
    return item
  def close_spider(self):
    self.cursor.close()
    self.connect.close()

在設(shè)置中打開:

ITEM_PIPELINES = {
  'taobaoSpider.pipelines.TaobaospiderPipeline': 300,
}

因為在上一步,我們已經(jīng)將管道傳送設(shè)置打開,所以這一步可以不用重復(fù)操作。

然后運行程序,打開數(shù)據(jù)庫查看數(shù)據(jù)。

使用Scrapy怎么對動態(tài)數(shù)據(jù)進(jìn)行爬取

至此,程序結(jié)束。

下附spider爬蟲文件所有代碼:

# -*- coding: utf-8 -*-
import scrapy
from selenium import webdriver
from ..items import TaobaospiderItem
class TaobaoSpider(scrapy.Spider):
  name = 'taobao'
  allowed_domains = ['taobao.com']
  start_urls = ['https://s.taobao.com/search?q=%E7%AC%94%E8%AE%B0%E6%9C%AC%E7%94%B5%E8%84%91&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306']
  def __init__(self):
    self.driver = webdriver.PhantomJS()
  def parse(self, response):
    div_info = response.xpath('//div[@class="info-cont"]')
    print(div_info)
    for div in div_info:
      title = div.xpath('.//div[@class="title-row "]/a/text()').extract_first('')
      print('名稱:', title)
      price = div.xpath('.//div[@class="sale-row row"]/div/span[2]/strong/text()').extract_first('')
      item = TaobaospiderItem()
      item['name'] = title
      item['price'] = price
      yield item
  def close(self,reason):
    print('結(jié)束了',reason)
    self.driver.quit()

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

當(dāng)前名稱:使用Scrapy怎么對動態(tài)數(shù)據(jù)進(jìn)行爬取-創(chuàng)新互聯(lián)
瀏覽路徑:http://www.chinadenli.net/article42/dishec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化電子商務(wù)用戶體驗網(wǎng)頁設(shè)計公司靜態(tài)網(wǎng)站App設(shè)計

廣告

聲明:本網(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)

商城網(wǎng)站建設(shè)