使用Scrapy爬取网站

6

我正在尝试使用Scrapy爬取一个网站,但是我在从该网站抓取所有产品时遇到了无限滚动的问题...

我只能抓取52个项目的以下数据,但总共有3824个项目。

hxs.select("//span[@class='itm-Catbrand strong']").extract()
hxs.select("//span[@class='itm-price ']").extract()
hxs.select("//span[@class='itm-title']").extract()

如果我使用hxs.select("//div[@id='content']/div/div/div").extract() 那么它会提取整个项目列表,但不会进一步筛选...我该如何爬取所有项目?
我已经尝试过这个方法,但结果相同。我错在哪里了?
def parse(self, response):
    filename = response.url.split("/")[-2]
    open(filename, 'wb').write(response.body
    for n in [2,3,4,5,6]:            
    req = Request(url="http://www.jabong.com/men/shoes/?page=" + n,
                      headers = {"Referer": "http://www.jabong.com/men/shoes/",
                                 "X-Requested-With": response.header['X-Requested-With']})
    return req 

我不确定 response.header['X-Requested-With'] 是否等于 "XMLHttpRequest",所以网站可能会将您重定向到(或提供)原始项目页面。此外,您应该使用 yield req 或将所有请求放入列表中。 - Xion345
1
如何设置您先前提到的头文件...并且使用yield/return时,我出现了这个错误 ERROR: Spider must return Request, BaseItem or None, got 'Request' in <GET http://www.jabong.com/men/shoes/> - Vaibhav Jain
1个回答

5

如您所猜测的那样,此网站使用JavaScript在您滚动页面时加载更多项目。

通过使用浏览器中包含的开发人员工具(对于Chromium浏览器,请按Ctrl-Maj i),我在网络选项卡中看到页面中包含的JavaScript脚本执行以下请求以加载更多项目:

GET http://www.website-your-are-crawling.com/men/shoes/?page=2 # 2,3,4,5,6 etc...

网络服务器响应以下类型的文档:
<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">&nbsp;</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>

这些文档包含更多的项目。
因此,为了获取完整的项目列表,您需要在Spider的parse方法中返回Request对象(请参见Spider类文档),以告诉scrapy它应该加载更多数据:
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

顺便提一下(如果您想测试),您不能只在浏览器中加载http://www.website-your-are-crawling.com/men/shoes/?page=2以查看返回的内容,因为如果X-Requested-With标头与XMLHttpRequest不同,该网站将重定向您到全局页面(即http://www.website-your-are-crawling.com/men/shoes/)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接