【问题标题】:Recursively Scraping Web Pages With Scrapy使用 Scrapy 递归抓取网页
【发布时间】:2014-09-15 21:05:09
【问题描述】:

http://www.example.com/listing.php?num=2&

这是我的蜘蛛代码,它在单个页面上显示链接列表:

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/”,我没有看到 在此 restrict_xpaths 中匹配的 xpath (class= "nextpage doies 代码源中不存在)

这是您的链接示例的规则:

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

【问题讨论】:

    标签: python recursion scrapy


    【解决方案1】:

    【讨论】:

    • 链接可能会过时。最好在此处包含重要部分以供参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    相关资源
    最近更新 更多