ウェブクローラ:クローラーは、情報ウェブサイトのコンテンツを取るために自動化されたプログラムであり、広く使われている検索エンジンやデータマイニングなどの分野です。
実行のWebクローラの基本的な流れ:ダウンロードのページ-データ抽出・ページ-リンクExtractorのページ
Scrapyは:、使いやすいクロスプラットフォーム、柔軟で拡張しやすいなど:Python言語の機能で書かれたオープンソースのWebクローラフレームワークです。
インストール
ネイティブ環境マック10.14、のpython3 pip3 install scrapy
成功したインストール後にscrapy -h
ビューはコマンドが含まれています
プロジェクトの作成scrapy startproject tutorial
以下のファイルを生成します
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
middlewares.py # project middlewares file
pipelines.py # project pipelines file
settings.py # project settings file
spiders/ # a directory where you'll later put your spiders
__init__.py
あなたの最初の爬虫類を書きます
実際には、クラスを記述し
たファイルを作成することquotes_spider.py
にtutorial/spiders
ディレクトリ
import scrapy
class QuotesSpider(scrapy.Spider):
# 爬虫名,必须唯一
name = "quotes"
def start_requests(self):
urls = [
'http://quotes.toscrape.com/page/1/',
'http://quotes.toscrape.com/page/2/',
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = 'quotes-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
self.log('Saved file %s' % filename)
プロジェクトのルートディレクトリの実装にscrapy crawl quotes
表示処理
結果:htmlファイルのうち二つ以上、我々はページを下にクロールしていることを意味します。
ます。https://www.jianshu.com/p/90ded0d8787fで再現