2016-11-20 6 views
0

Ich bin neu zu programmieren auf Python und mit scrapy arbeiten. Ich bin dabei, eine Webseite zu crawlen und dann die Sammlung in mongoDB zu speichern. Ich habe einen Fehler beim Crawlen im Web. Ich habe ähnliche Hilfeseiten auf dieser Seite verwendet und sogar ein Tutorial von Anfang bis Ende vergeblich verfolgt, jede Hilfe wird geschätzt.Scrapy Spider Fehler Verarbeitung

This is the error i'm getting from terminal, Spider error processing

Hier ist mein Code:

from scrapy.item import Item, Field 

#class 1 
class StackItem(Item): 
# define the fields for your item here like: 
# name = scrapy.Field() 
pagetitle = Field() 
newsmain = Field() 
pass 

from scrapy import Spider 
from scrapy.selector import Selector 
from stack.items import StackItem 

#class 2 
class StackSpider(Spider): 
name = "stack" 
allowed_domains = ["docs.python.org"] 
start_urls = ["https://docs.python.org/2/howto/curses.html",] 

def parse(self, response): 
    information = Selector(response.body).xpath('//div[@class="section"]') 

    for data in information: 
     item = StackItem() 
     item['pagetitle'] = data.information('//*[@id="curses-programming- with-python"]').extract() 
     item['newsmain'] = data.information('//*[@id="what-is- curses"]').extract() 

    yield item 
+0

Können Sie den Einzug Ihrer Code einfügen? –

Antwort

0

scrapy.selector.Selector.__init__()expects a Response object as first argument.

Wenn Sie einen Selektor für eine HTTP-Antwort Körper zu bauen, verwenden Sie das text= Argument:

$ scrapy shell https://docs.python.org/2/howto/curses.html 
2016-11-21 11:05:34 [scrapy] INFO: Scrapy 1.2.1 started (bot: scrapybot) 
(...) 
2016-11-21 11:05:35 [scrapy] INFO: Spider opened 
2016-11-21 11:05:35 [scrapy] DEBUG: Crawled (200) <GET https://docs.python.org/2/howto/curses.html> (referer: None) 
(...) 
>>> 
>>> # 
>>> # passing response.body (bytes) instead of a Response object fails 
>>> # 
>>> scrapy.Selector(response.body) 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/home/paul/.virtualenvs/scrapy12/local/lib/python2.7/site-packages/scrapy/selector/unified.py", line 67, in __init__ 
    text = response.text 
AttributeError: 'str' object has no attribute 'text' 
>>> 
>>> # 
>>> # use text= argument to pass response body 
>>> # 
>>> scrapy.Selector(text=response.body) 
<Selector xpath=None data=u'<html xmlns="http://www.w3.org/1999/xhtm'> 
>>> 
>>> scrapy.Selector(text=response.body).xpath('//div[@class="section"]') 
[<Selector xpath='//div[@class="section"]' data=u'<div class="section" id="curses-programm'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="what-is-curses"'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="the-python-curs'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="starting-and-en'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="windows-and-pad'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="displaying-text'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="attributes-and-'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="user-input">\n<h'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="for-more-inform'>] 
>>> 

Ein einfacher Weg ist, direkt die Antwort Objekt zu übergeben:

>>> scrapy.Selector(response).xpath('//div[@class="section"]') 
[<Selector xpath='//div[@class="section"]' data=u'<div class="section" id="curses-programm'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="what-is-curses"'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="the-python-curs'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="starting-and-en'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="windows-and-pad'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="displaying-text'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="attributes-and-'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="user-input">\n<h'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="for-more-inform'>] 

und ein noch einfacherer Weg ist die Verwendung .xpath() method on the response instance (es ist eine bequeme Methode, die einen Selector für Sie erstellt), vorausgesetzt, Ihre Antwort ist eine HtmlResponse oder XmlResponse (die in der Regel für Web Scraping ist)

>>> response.xpath('//div[@class="section"]') 
[<Selector xpath='//div[@class="section"]' data=u'<div class="section" id="curses-programm'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="what-is-curses"'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="the-python-curs'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="starting-and-en'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="windows-and-pad'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="displaying-text'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="attributes-and-'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="user-input">\n<h'>, <Selector xpath='//div[@class="section"]' data=u'<div class="section" id="for-more-inform'>] 
+0

@paulmbrth Vielen Dank für die Antwort Ich werde Ihren Vorschlag jetzt versuchen, auch habe ich nicht erkannt, dass es ein Problem mit der Einrückung gab ich werde eine Notiz für das nächste Mal nehmen – Daniel

+0

Ich habe nur den Vorschlag versucht und es funktioniert nicht, ich bin sicher ein Fehler von meinem Ende, wenn du kannst, hast du irgendwelche Vorschläge zu den Änderungen, die ich an meinem ursprünglichen Code vornehmen muss, damit das funktioniert? ... Ich sehe, du verwendest Shell und ich habe versucht, Shell auszuführen, um mir aber zu helfen Ich leide unter dem _curses Importfehler – Daniel

Verwandte Themen