python - 使用 scrapy 在多页上递归获取链接

标签 python web-scraping scrapy

我正在使用我在网上找到的以下代码来递归地抓取多个页面上的链接。它应该递归地返回所有页面上我需要的所有链接。但我最终最多只获得 100 个链接。任何建议都会有帮助。

class MySpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["craigslist.org"]
    start_urls = ["http://seattle.craigslist.org/search/jjj?is_parttime=1"]   

    rules = (Rule (SgmlLinkExtractor(allow=("index\d00\.html", ),restrict_xpaths=('//a[@class="button next"]',))
    , callback="parse_items", follow= True),
    )

    def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select('//span[@class="pl"]')
        items = []
        for titles in titles:
            item = CraigslistSampleItem()
            item ["title"] = titles.select("a/text()").extract()
            item ["link"] = titles.select("a/@href").extract()           
            items.append(item)     
        return(items)

最佳答案

只需删除 allow=("index\d00\.html", ) 即可让它解析 next 链接:

rules = (Rule(SgmlLinkExtractor(restrict_xpaths=('//a[@class="button next"]',)),           
              callback="parse_items", follow= True),)

关于python - 使用 scrapy 在多页上递归获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493773/

相关文章:

python - Scrapy yield 项目作为 JSON 中的子项目

java - 如何将 CRLF 转换为\n 字符串

python - 将 pdf 转换为图像时出现类型错误 : TypeError ("object of type ' NoneType' has no len()", )

python - 虚拟变量水平不存在于未见数据中

python - 在提取/分解一些 'td' 标签后,无法访问超出表第一行的 'td' 标签

python - 为什么当我运行 `sel` 时,选择器 `scrapy shell` 不存在?

python - 迭代文件行并保持计数器的最简单方法是什么?

html - 抓取整个网站

python - 无法解析用户名以确保我已登录网站

django - 将Scrapy与Django 1.9集成 : django. core.exceptions.AppRegistryNotReady:应用程序尚未加载