Scrapy-extracting-items
スクレイピー-アイテムの抽出
説明
Webページからデータを抽出するために、Scrapyはhttps://www.w3.org/TR/xpath/[XPath]およびhttps://www.w3.org/TR/selectors/[CSS]に基づくセレクターと呼ばれる手法を使用します式。 以下は、XPath式のいくつかの例です-
- /html/head/title -これにより、HTMLドキュメントの<head>要素内の<title>要素が選択されます。
- /html/head/title/text()-これは、同じ<title>要素内のテキストを選択します。
- //td -これは、<td>からすべての要素を選択します。
- //div [@class = "slice"] -これは_div_から属性class = "slice"を含むすべての要素を選択します
セレクタには、次の表に示す4つの基本的な方法があります-
Sr.No | Method & Description |
---|---|
1 |
extract() 選択したデータとともにUnicode文字列を返します。 |
2 |
re() 正規表現が引数として与えられたときに抽出されたUnicode文字列のリストを返します。 |
3 |
xpath() 引数として指定されたxpath式で選択されたノードを表すセレクターのリストを返します。 |
4 |
css() 引数として指定されたCSS式で選択されたノードを表すセレクターのリストを返します。 |
シェルでセレクターを使用する
組み込みのScrapyシェルでセレクターをデモンストレーションするには、システムにhttps://ipython.org/[IPython]がインストールされている必要があります。 ここで重要なことは、Scrapyの実行中にURLを引用符に含める必要があることです。そうしないと、「&」文字を含むURLは機能しません。 あなたは、プロジェクトのトップレベルディレクトリで次のコマンドを使用してシェルを開始することができます-
scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
シェルは次のようになります-
[ ... Scrapy log here ... ]
2014-01-23 17:11:42-0400 [scrapy] DEBUG: Crawled (200)
<GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>(referer: None)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x3636b50>
[s] item {}
[s] request <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s] response <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s] settings <scrapy.settings.Settings object at 0x3fadc50>
[s] spider <Spider 'default' at 0x3cebf50>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
In [1]:
シェルがロードされると、それぞれ_response.body_と_response.header_を使用して、本文またはヘッダーにアクセスできます。 同様に、_response.selector.xpath()_または_response.selector.css()_を使用して、応答に対してクエリを実行できます。
例えば-
In [1]: response.xpath('//title')
Out[1]: [<Selector xpath = '//title' data = u'<title>My Book - Scrapy'>]
In [2]: response.xpath('//title').extract()
Out[2]: [u'<title>My Book - Scrapy: Index: Chapters</title>']
In [3]: response.xpath('//title/text()')
Out[3]: [<Selector xpath = '//title/text()' data = u'My Book - Scrapy: Index:'>]
In [4]: response.xpath('//title/text()').extract()
Out[4]: [u'My Book - Scrapy: Index: Chapters']
In [5]: response.xpath('//title/text()').re('(\w+):')
Out[5]: [u'Scrapy', u'Index', u'Chapters']
データの抽出
通常のHTMLサイトからデータを抽出するには、サイトのソースコードを調べてXPathを取得する必要があります。 検査後、データが ul タグにあることがわかります。 li タグ内の要素を選択します。
コードの次の行は、さまざまな種類のデータの抽出を示しています-
liタグ内のデータを選択する場合-
response.xpath('//ul/li')
説明を選択するために-
response.xpath('//ul/li/text()').extract()
サイトのタイトルを選択するために-
response.xpath('//ul/li/a/text()').extract()
サイトリンクを選択するために-
response.xpath('//ul/li/a/@href').extract()
次のコードは、上記の抽出の使用を示しています-
import scrapy
class MyprojectSpider(scrapy.Spider):
name = "project"
allowed_domains = ["dmoz.org"]
start_urls = [
"http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
"http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
]
def parse(self, response):
for sel in response.xpath('//ul/li'):
title = sel.xpath('a/text()').extract()
link = sel.xpath('a/@href').extract()
desc = sel.xpath('text()').extract()
print title, link, desc