2017-08-09 4 views
0

Nach einem Tutorial auf youtube: Scraping Web Pages with Scrapy Python Web-Crawling-Skript, folgende Tutorial und mit Fragen

Es ist alt, für Python 2.x und ich lerne Version 3.x. Bisher habe ich ein paar Probleme mit Google kennengelernt. Ich bin jedoch noch einen Fehler bekommen:

File "/usr/lib64/python3.5/site-packages/twisted/internet/defer.py", line 653, in _runCallbacks current.result = callback(current.result, *args, **kw) File "/home/skeer/PycharmProjects/scrape_craigslists/scrape_cl/scrape_cl/spiders/scrape.py", line 11, in parse xpath = scrapy.selector(response) TypeError: 'module' object is not callable

googeln früher fand ich Verweise auf andere, die dies zu einem nicht aktivierten Charakter zurückzuführen war, als ob die ‚s‘ in der Wahl Hauptstadt sein sollte. Versuchte das und wurde mit einem Fehler darüber begrüßt, wie scrapy.Selector-Modul nicht gefunden werden konnte.

Hier ist mein Code:

from scrapy.spider import Spider 
import scrapy.selector 


class MySpider(Spider): 
name = "craigslist" 
allowed_domains = ["craigslist.org"] 
start_urls = ["https://helena.craigslist.org/search/sad"] 

def parse(self, response): 
    xpath = scrapy.selector(response) 
    titles = xpath.select("//p") 
    for titles in titles: 
     title = xpath("/body/section/form/div/li/p[@class]()").extract()  
     link = 
xpath("/body/section/form/div/ul/li/a[@href]").extract() 
     print (title, link) 

Antwort

0

scrapy.selector ist das Modul Selektoren enthält. Versuchen

from scrapy.selector import Selector 

dies jedoch nicht notwendig ist, weil das Antwortobjekt bereits selector interface and an xpath method hat, so sollten Sie tun:

def parse(self, response): 
    xpath = response.xpath 
    titles = xpath("//p") 
    for titles in titles: 
     title = xpath("/body/section/form/div/li/p[@class]()").extract()  
     link = xpath("/body/section/form/div/ul/li/a[@href]").extract() 
     print (title, link) 

zusätzlich, müssen Sie eine sehr gute Liste von Proxies, wenn Sie planen, Craigslist scrappen. Sie verbieten ip schnell, speziell um Kratzen zu verhindern.

0

die Funktionsdefinition Ändert:

def parse(self, response): 
    xpath = scrapy.selector.Selector(response) 
    titles = xpath.select("//p") 
    for titles in titles: 
     title = xpath.xpath("/body/section/form/div/li/p[@class]()").extract() 
     link = xpath.xpath("/body/section/form/div/ul/li/a[@href]").extract() 
     print(title, link) 

Hinweis xpath("/body/section/form/div/li/p[@class]()") ->xpath.xpath("/body/section/form/div/li/p[@class]()")