2019ディジタルデザインIII 「Scrapyやtwitterscraperを用いた情報収集」

1. Scrapyのロボットの設計
  • プロジェクトの概要
  • 以下のファイルが自動で生成される
  • ---
  • tutorial/
  •     scrapy.cfg            # deploy configuration file
  •     tutorial/             # project's Python module, you'll import your code from here
  •         __init__.py
  •         items.py          # project items definition file
  •         pipelines.py      # project pipelines file
  •         settings.py       # project settings file
  •         spiders/          # a directory where you'll later put your spiders
  •             __init__.py
  • ---
  • プロジェクトの作成
  • まず、プロジェクトを作成するフォルダを定義する
  • Windowsでは"C:\python"、Macでは"/documents/python"など
  • コマンドプロンプト(Macではターミナル)で、以下のコマンドでScrapyの新しいプロジェクトを開始する。前述のプロジェクトフォルダの直下で実行する。
  • $ scrapy startproject <プロジェクトの名称>
  • 合わせて以下のウェブサイトを参照
  • スパイダーの作成
  • $ cd tutorial
  • $ scrapy genspider <スパイダーの名称> <スパイダーがアクセスするドメイン>
  • 例: $ scrapy genspider nii nii.ac.jp
  • 作成されたスパイダーを編集し、ロボットを開発する
# -*- coding: utf-8 -*-
import scrapy
import datetime
import re

from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from pediadata.items import PediadataItem
from scrapy.selector import Selector

class DataSpider(scrapy.Spider):
 name = 'data'
 allowed_domains = ['wikipedia.org']
 f = open("list.txt", encoding="utf-8")
 start_urls = [url.strip() for url in f.readlines()]
 f.close()

 def parse(self, response):
 item = PediadataItem()