あなたの声明
response = HtmlResponse(url=URL)
は、空のボディと、a "local scope" HtmlResponse
objectを構築します。それは何もダウンロードしません。特にリソースはhttp://doc.scrapy.org/en/latest/_static/selectors-sample1.htmlではありません。
通常、自分でHtmlResponse
オブジェクトを作成することはありません。あなたが与えたRequest
インスタンスの処理が終了したら、Scrapyフレームワークでそれらを構築します。 Request(url='http://doc.scrapy.org/en/latest/_static/selectors-sample1.html')
あなたがScrapyを試している場合は、私はあなたがscrapy shell
と遊ぶお勧め:fetch('http://someurl')
を使用して対話型シェルの内側に、あなたがダウンロードをトリガすることができます(とで動作するように「本物の」Response
オブジェクトを取得):
$ scrapy shell
2016-06-14 10:59:31 [scrapy] INFO: Scrapy 1.1.0 started (bot: scrapybot)
(...)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x7f1a6591d588>
[s] item {}
[s] settings <scrapy.settings.Settings object at 0x7f1a6ce290f0>
[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
>>> fetch('http://doc.scrapy.org/en/latest/_static/selectors-sample1.html')
2016-06-14 10:59:51 [scrapy] INFO: Spider opened
2016-06-14 10:59:51 [scrapy] DEBUG: Crawled (200) <GET http://doc.scrapy.org/en/latest/_static/selectors-sample1.html> (referer: None)
>>> response.xpath('//title/text()').extract()
['Example website']
- サブクラス
scrapy.Spider
、
- を請うにURLを定義します。シェルの外で、実際にデータをダウンロードするには、以下を行う必要があり以下からのダウンロードで、
- やダウンロードしたデータを作業するコールバックメソッドを記述し、
Response
オブジェクト内にラップそれらに渡されること
非常に簡単な例(と呼ばれるファイルで、たとえば、test.py
:
import scrapy
class TestSpider(scrapy.Spider):
name = 'testspider'
# start_urls is special and internally it builds Request objects for each of the URLs listed
start_urls = ['http://doc.scrapy.org/en/latest/_static/selectors-sample1.html']
def parse(self, response):
yield {
'title': response.xpath('//h1/text()').extract_first()
}
次に、スパイダーを実行する必要があります。Scrapyは、単一ファイルのクモを実行するためのコマンドがあります。
$ scrapy runspider test.py
をそして、あなたはあなたのコンソールでこれを取得する:
2016-06-14 10:48:05 [scrapy] INFO: Scrapy 1.1.0 started (bot: scrapybot)
2016-06-14 10:48:05 [scrapy] INFO: Overridden settings: {}
2016-06-14 10:48:06 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.logstats.LogStats', 'scrapy.extensions.corestats.CoreStats']
2016-06-14 10:48:06 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
'scrapy.downloadermiddlewares.retry.RetryMiddleware',
'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-06-14 10:48:06 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
'scrapy.spidermiddlewares.referer.RefererMiddleware',
'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-06-14 10:48:06 [scrapy] INFO: Enabled item pipelines:
[]
2016-06-14 10:48:06 [scrapy] INFO: Spider opened
2016-06-14 10:48:06 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-06-14 10:48:06 [scrapy] DEBUG: Crawled (200) <GET http://doc.scrapy.org/en/latest/_static/selectors-sample1.html> (referer: None)
2016-06-14 10:48:06 [scrapy] DEBUG: Scraped from <200 http://doc.scrapy.org/en/latest/_static/selectors-sample1.html>
{'title': 'Example website'}
2016-06-14 10:48:06 [scrapy] INFO: Closing spider (finished)
2016-06-14 10:48:06 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 252,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 501,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2016, 6, 14, 8, 48, 6, 564591),
'item_scraped_count': 1,
'log_count/DEBUG': 2,
'log_count/INFO': 7,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2016, 6, 14, 8, 48, 6, 85693)}
2016-06-14 10:48:06 [scrapy] INFO: Spider closed (finished)
あなたが本当に実際に任意のWebデータをダウンロードすることなく、セレクターで再生したい場合は、仮定しますあなたは(あなたのブラウザでview-source:
から例えば、コピー用)がすでにローカルにデータを持っている、あなたがそれを行うことができますが、体を供給する必要があります。
>>> response = HtmlResponse(url=URL, body='''
... <!DOCTYPE html>
... <html>
... <head>
... </head>
... <body>
... <h1>Herman Melville - Moby-Dick</h1>
...
... <div>
... <p>
... Availing himself of the mild, summer-cool weather that now reigned in these latitudes, ... them a care-killing competency.
... </p>
... </div>
... </body>
... </html>''', encoding='utf8')
>>> response.xpath('//h1')
[<Selector xpath='//h1' data='<h1>Herman Melville - Moby-Dick</h1>'>]
>>> response.xpath('//h1').extract()
['<h1>Herman Melville - Moby-Dick</h1>']
>>>
scrapyあなた寿を最大限に活用するにはldチュートリアルに従うと、レスポンスオブジェクトはリクエストからリクエストまで自動的に構築されます –