Come avrete intuito, questo sito utilizza Javascript per caricare più elementi quando si scorre la pagina di .
Utilizzando gli sviluppatori strumenti inclusi nel mio browser (Ctrl-Maj i per il cromo), ho visto nella scheda Rete che lo script javascript incluso nella pagina esegue le seguenti richieste per caricare più elementi:
GET http://www.website-your-are-crawling.com/men/shoes/?page=2 # 2,3,4,5,6 etc...
il server web risponde con i documenti del seguente tipo:
<li id="PH969SH70HPTINDFAS" class="itm hasOverlay unit size1of4 ">
<div id="qa-quick-view-btn" class="quickviewZoom itm-quickview ui-buttonQuickview l-absolute pos-t" title="Quick View" data-url ="phosphorus-Black-Moccasins-233629.html" data-sku="PH969SH70HPTINDFAS" onClick="_gaq.push(['_trackEvent', 'BadgeQV','Shown','OFFER INSIDE']);">Quick view</div>
<div class="itm-qlInsert tooltip-qlist highlightStar"
onclick="javascript:Rocket.QuickList.insert('PH969SH70HPTINDFAS', 'catalog');
return false;" >
<div class="starHrMsg">
<span class="starHrMsgArrow"> </span>
Save for later </div>
</div>
<a id='cat_105_PH969SH70HPTINDFAS' class="itm-link sobrTxt" href="/phosphorus-Black-Moccasins-233629.html"
onclick="fireGaq('_trackEvent', 'Catalog to PDP', 'men--Shoes--Moccasins', 'PH969SH70HPTINDFAS--1699.00--', this),fireGaq('_trackEvent', 'BadgePDP','Shown','OFFER INSIDE', this);">
<span class="lazyImage">
<span style="width:176px;height:255px;" class="itm-imageWrapper itm-imageWrapper-PH969SH70HPTINDFAS" id="http://static4.jassets.com/p/Phosphorus-Black-Moccasins-6668-926332-1-catalog.jpg" itm-img-width="176" itm-img-height="255" itm-img-sprites="4">
<noscript><img src="http://static4.jassets.com/p/Phosphorus-Black-Moccasins-6668-926332-1-catalog.jpg" width="176" height="255" class="itm-img"></noscript>
</span>
</span>
<span class="itm-budgeFlag offInside"><span class="flagBrdLeft"></span>OFFER INSIDE</span>
<span class="itm-Catbrand strong">Phosphorus</span>
<span class="itm-title">
Black Moccasins </span>
Questi documenti contengono più elementi.
Quindi, per ottenere l'elenco completo degli articoli si dovrà tornare Request
oggetti nel metodo del vostro Spider parse
(Vedere la Spider class documentation), a dire Scrapy che dovrebbe caricare più dati:
def parse(self, response):
# ... Extract items in the page using extractors
n = number of the next "page" to parse
# You get get n by using response.url, extracting the number
# at the end and adding 1
# It is VERY IMPORTANT to set the Referer and X-Requested-With headers
# here because that's how the website detects if the request was made by javascript
# or direcly by following a link.
req = Request(url="http://www.website-your-are-crawling.com/men/shoes/?page=" + n,
headers = {"Referer": "http://www.website-your-are-crawling.com/men/shoes/",
"X-Requested-With": "XMLHttpRequest"})
return req # and your items
Oh, e comunque (nel caso tu voglia testare), non puoi semplicemente caricare http://www.website-your-are-crawling.com/men/shoes/?page=2
nel tuo browser per vedere cosa restituisce perché il sito ti reindirizzerà alla pagina globale (ovvero http://www.website-your-are-crawling.com/men/shoes/
) se l'intestazione X-Requested-With
è diversa da XMLHttpRequest
.
Non sono sicuro che 'response.header ['X-Requested-With']' sia uguale a "XMLHttpRequest", quindi il sito Web potrebbe reindirizzare (o servire) la pagina degli elementi originali. Inoltre dovresti probabilmente usare 'yield req' o mettere tutte le richieste in una lista. – Xion345
Come impostare l'intestazione che hai menzionato in precedenza ... e usando yield/return sto ricevendo questo errore 'ERRORE: Spider deve restituire Request, BaseItem o None, ha ottenuto 'Request' in ' –