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。而且,即使我再次運行它,它仍然會一直廢棄相同的頁面,但並非總是如此。任何想法可能是我的方法錯誤或我錯過了什麼?
在此先感謝。
我試着看看這些頁面的源代碼,看起來像結果稍後加載並繼續顯示「加載」。當我們嘗試查看源代碼時,會發生類似的事情。它顯示一個「加載 - 小」,並在一段時間後才加載。因此,我的抓取工具在開始抓取時沒有找到任何要抓取的數據。任何解決方案? – Aryabhatt
如果結果稍後加載,所以必須有一些javascript使用ajax加載 - 你可以在javascript中搜索單詞「ajax」,「post」,「get」或「http://」以查找加載數據的URL。我也使用firefox + firebug來查看瀏覽器調用哪些url--它比在javascript中搜索更快。如果你有一些網址,你可以測試它並直接使用它來獲取數據。 – furas