2013-05-31 28 views
1

抓取數據:Scrapy爬蟲無法從我試圖取消下頁的結果多頁

http://www.peekyou.com/work/autodesk/page=1

與頁= 1,2,3,4 ......等爲每結果。所以我得到一個php文件來運行爬蟲運行它爲不同的頁碼。的代碼(用於單個頁)如下:

`import sys 
from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 
from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from scrapy.item import Item 
from scrapy.http import Request 
#from scrapy.crawler import CrawlerProcess 

class DmozSpider(BaseSpider): 
name = "peekyou_crawler" 

start_urls = ["http://www.peekyou.com/work/autodesk/page=1"]; 

def parse(self, response): 

    hxs = HtmlXPathSelector(response) 

    discovery = hxs.select('//div[@class="nextPage"]/table/tr[2]/td/a[contains(@title,"Next")]') 
    print len(discovery) 

    print "Starting the actual file" 
    items = hxs.select('//div[@class="resultCell"]') 
    count = 0 
    for newsItem in items: 
     print newsItem 

     url=newsItem.select('h2/a/@href').extract() 
     name = newsItem.select('h2/a/span/text()').extract() 
     count = count + 1 
     print count 
     print url[0] 
     print name[0] 

     print "\n" 

` 所述的Autodesk結果頁面有18頁。當我運行代碼抓取所有頁面時,抓取工具只從第2頁獲取數據,而不是所有頁面。同樣,我將公司名稱改爲別的。再一次,它將一些頁面剪下,然後休息一下。儘管我在每個頁面上都獲得了http響應200。而且,即使我再次運行它,它仍然會一直廢棄相同的頁面,但並非總是如此。任何想法可能是我的方法錯誤或我錯過了什麼?

在此先感謝。

回答

1

您可以添加更多的地址:

start_urls = [ 
    "http://www.peekyou.com/work/autodesk/page=1", 
    "http://www.peekyou.com/work/autodesk/page=2", 
    "http://www.peekyou.com/work/autodesk/page=3" 
]; 

您可以生成更多的地址:

start_urls = [ 
    "http://www.peekyou.com/work/autodesk/page=%d" % i for i in xrange(18) 
]; 

我想你應該閱讀有關start_requests()以及如何生成下一個網址。但我在這裏幫不了你,因爲我不使用Scrapy。我仍然使用純python(和pyQuery)來創建簡單的爬蟲;)

PS。有時服務器會檢查你的UserAgent,IP,你下一頁的速度有多快,並停止向你發送頁面。

+0

我試着看看這些頁面的源代碼,看起來像結果稍後加載並繼續顯示「加載」。當我們嘗試查看源代碼時,會發生類似的事情。它顯示一個「加載 - 小」,並在一段時間後才加載。因此,我的抓取工具在開始抓取時沒有找到任何要抓取的數據。任何解決方案? – Aryabhatt

+0

如果結果稍後加載,所以必須有一些javascript使用ajax加載 - 你可以在javascript中搜索單詞「ajax」,「post」,「get」或「http://」以查找加載數據的URL。我也使用firefox + firebug來查看瀏覽器調用哪些url--它比在javascript中搜索更快。如果你有一些網址,你可以測試它並直接使用它來獲取數據。 – furas

1

我給你一個出發點。

您嘗試抓取的頁面通過AJAX加載,這是scrapy的問題 - 它無法通過ajax XHR請求處理動態頁面加載。欲瞭解更多信息,請參閱:

使用瀏覽器的開發者工具,你可以注意到有傳出POST請求的頁面加載後。這是去http://www.peekyou.com/work/autodesk/web_results/web_tag_search_checker.php

所以,在scrapy模擬這應該幫助你抓取所需的數據:

from scrapy.http import FormRequest 
from scrapy.item import Item, Field 
from scrapy.spider import BaseSpider 
from scrapy.selector import HtmlXPathSelector 


class DmozItem(Item): 
    name = Field() 
    link = Field() 


class DmozSpider(BaseSpider): 
    name = "peekyou_crawler" 

    start_urls = start_urls = [ 
     "http://www.peekyou.com/work/autodesk/page=%d" % i for i in xrange(18) 
    ] 

    def parse(self, response): 
     yield FormRequest(url="http://www.peekyou.com/work/autodesk/web_results/web_tag_search_checker.php", 
          formdata={'id': 'search_work_a10362ede5ed8ed5ff1191321978f12a', 
            '_': ''}, 
          method="POST", 
          callback=self.after_post) 

    def after_post(self, response): 
     hxs = HtmlXPathSelector(response) 

     persons = hxs.select("//div[@class='resultCell']") 

     for person in persons: 
      item = DmozItem() 
      item['name'] = person.select('.//h2/a/span/text()').extract()[0].strip() 
      item['link'] = person.select('.//h2/a/@href').extract()[0].strip() 
      yield item 

它的工作原理,但它轉儲僅在第一頁。我會讓你知道你怎麼能得到其他結果。

希望有所幫助。