2014-07-24 99 views
0

http://www.example.com/listing.php?num=2&遞歸刮Web頁面Scrapy

這裏是顯示在一個頁面上的鏈接列表中的我的蜘蛛代碼:

from scrapy.log import * 
from crawler_bhinneka.settings import * 
from crawler_bhinneka.items import * 
import pprint 
from MySQLdb import escape_string 
import urlparse 

def complete_url(string): 
    """Return complete url""" 
    return "http://www.example.com" + string 


class BhinnekaSpider(CrawlSpider): 

    name = 'bhinneka_spider' 
    start_urls = [ 
     'http://www.example.com/listing.php?' 
    ] 
    def parse(self, response): 

     hxs = HtmlXPathSelector(response) 

     # HXS to find url that goes to detail page 
     items = hxs.select('//td[@class="lcbrand"]/a/@href') 
     for item in items: 
      link = item.extract() 
      print("my Url Link : ",complete_url(link)) 

知道我可以得到我的第一個所有鏈接頁。

我想通過遞歸規則使用這個蜘蛛來跟隨下一頁的鏈接 你知道如何在蜘蛛中嘗試我的規則來獲取下一頁的鏈接值。

編輯

@Toan,感謝你的回覆。 我試圖讓你發給我的這個教程鏈接,但我只是把一個頁面(第一頁)的項目值。

我看了看源代碼在這個網址: 「http://sfbay.craigslist.org/npo/」我沒有看到 的XPath,在這種restrict_xpaths(類=「下一頁doies 不在代碼源存在)

匹配的值

這裏是你的規則聯繫起來,例如:

rules = (Rule (SgmlLinkExtractor (allow = ("index \ d00 \. html") restrict_xpaths = ('//p [@ class = "nextpage"]')) 
    , Callback = "parse_items" follow = True) 
    ) 

回答