.xpath().extract()
e .css().extract()
restituire un elenco perché .xpath()
e .css()
ritorno SelectorList
oggetti.
Vedi https://parsel.readthedocs.org/en/v1.0.1/usage.html#parsel.selector.SelectorList.extract
(SelectorList) .extract():
Chiamare la .extract() metodo per ogni elemento è questa lista e restituire i loro risultati appiattite, come un elenco di stringhe Unicode.
.extract_first()
è quello che state cercando (che è scarsamente documentata)
Tratto da http://doc.scrapy.org/en/latest/topics/selectors.html:
Se si desidera estrarre solo primo elemento abbinato, è possibile chiamare il selettore .extract_first()
>>> response.xpath('//div[@id="images"]/a/text()').extract_first()
u'Name: My image 1 '
Nel vostro altro esempio:
def parse(self, response):
for href in response.css("ul.directory.dir-col > li > a::attr('href')"):
link = href.extract()
print(link)
ogni href
nel circuito sarà un oggetto Selector
.Chiamando .extract()
su di esso ti porterà una singola stringa Unicode indietro:
$ scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/"
2016-02-26 12:11:36 [scrapy] INFO: Scrapy 1.0.5 started (bot: scrapybot)
(...)
In [1]: response.css("ul.directory.dir-col > li > a::attr('href')")
Out[1]:
[<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>,
<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>,
...
<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>]
così .css()
sul response
restituisce un SelectorList
:
In [2]: type(response.css("ul.directory.dir-col > li > a::attr('href')"))
Out[2]: scrapy.selector.unified.SelectorList
Looping su quell'oggetto ti dà Selector
casi:
In [5]: for href in response.css("ul.directory.dir-col > li > a::attr('href')"):
...: print href
...:
<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>
<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>
(...)
<Selector xpath=u"descendant-or-self::ul[@class and contains(concat(' ', normalize-space(@class), ' '), ' directory ') and (@class and contains(concat(' ', normalize-space(@class), ' '), ' dir-col '))]/li/a/@href" data=u'/Computers/Programming/Languages/Python/'>
E chiamare .extract()
fornisce un singolo str Unicode ing:
In [6]: for href in response.css("ul.directory.dir-col > li > a::attr('href')"):
print type(href.extract())
...:
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
<type 'unicode'>
Nota: .extract()
su Selector
è wrongly documented in modo da restituire una lista di stringhe. Aprirò un problema su parsel
(che è lo stesso dei selettori di Scrapy e usato sotto il cofano in 1.1.)
Grazie per la risposta rapida! Ho appena modificato il post e ho aggiunto un esempio del tutorial in cui 'extract()' fornisce una stringa. È perché sto usando i CSS? – entron
Giusto, ciò che ho scritto non è corretto (troppo veloce di una risposta). In effetti '.xpath(). Extract()' e '.css(). Extract() restituisce liste perché' .xpath() 'e' .css() 'restituiscono oggetti' SelectorList'. Ma il ciclo su '.xpath()' ti dà un 'Selector', dal quale puoi chiamare' .extract() 'e ottenere un singolo elemento. Modificherò la mia risposta –
Questa parte è davvero confusa, ma ora capisco! Grazie mille! – entron