2017-05-27 68 views
0

ScrapyがローカルHTMLファイルをクロールするようにしたいが、ヘッダーにContent-typeフィールドがないのでスタックしている。結果の応答オブジェクトが含まれていません、それは(MacOSの上)のように見えるので、そこで、基本的に、私は、このようなfile:///Users/felix/myfile.htmlPython Scrapy:ローカルファイルからのクロール:Content-Type undefined

として、ローカルのURLにscrapyを指していますしかし、scrapyは、その後、クラッシュしますUse Scrapy to crawl local XML file - Start URL local file address:私はここのチュートリアルに従ってきました必須フィールドContent-type

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/felix/IdeaProjects/news-please/newsplease/__init__.py 
[scrapy.core.scraper:158|ERROR] Spider error processing <GET file:///Users/felix/IdeaProjects/news-please/newsplease/0a2199bdcef84d2bb2f920cf042c5919> (referer: None) 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/utils/defer.py", line 102, in iter_errback 
    yield next(it) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/offsite.py", line 29, in process_spider_output 
    for x in result: 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/referer.py", line 22, in <genexpr> 
    return (_set_referer(r) for r in result or()) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/urllength.py", line 37, in <genexpr> 
    return (r for r in result or() if _filter(r)) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scrapy/spidermiddlewares/depth.py", line 58, in <genexpr> 
    return (r for r in result or() if _filter(r)) 
    File "/Users/felix/IdeaProjects/news-please/newsplease/crawler/spiders/download_crawler.py", line 33, in parse 
    if not self.helper.parse_crawler.content_type(response): 
    File "/Users/felix/IdeaProjects/news-please/newsplease/helper_classes/parse_crawler.py", line 116, in content_type 
    if not re.match('text/html', response.headers.get('Content-Type').decode('utf-8')): 
AttributeError: 'NoneType' object has no attribute 'decode' 

誰かがPython Scrapy on offline (local) dataを参照して、簡単なhttpサーバを実行するために提案されたが、それは主な理由は別のサーバを実行することによって生じたオーバーヘッドのオプションではありません。

私は、スクラピーを使用するより大きなフレームワークを持っているので、まずはスクラピーを使用する必要があります。ローカルファイルからそのフレームワークにクロールする機能を追加する予定です。しかし、ローカルファイル(以前のリンクを参照してください)からクロールする方法については、SOに関するいくつかの質問があるので、私はこの問題が一般的な関心事であると考えています。

答えて

1

def content_type(self, response)return Trueには、実際にニュースをフォークするか、または変更することができます。newsplease/helper_classes/parse_crawler.pyはローカルストレージからのものです。

新しいファイルは次のようになります。

def content_type(self, response): 
    """ 
    Ensures the response is of type 

    :param obj response: The scrapy response 
    :return bool: Determines wether the response is of the correct type 
    """ 
    if response.url.startswith('file:///'): 
     return True 
    if not re.match('text/html', response.headers.get('Content-Type').decode('utf-8')): 
     self.log.warn("Dropped: %s's content is not of type " 
         "text/html but %s", response.url, 
         response.headers.get('Content-Type')) 
     return False 
    else: 
     return True 
+0

いや、それはオプションになりますが、私は私たちのフレームワークでそれらをパッケージ化する必要があるだろうとして、外部のライブラリを変更しないことを好むだろう。 – pedjjj

+0

その後、https://github.com/fhamborg/news-pleaseでバグを報告することができます。 –

関連する問題