在 Python 中进行 Web 爬网的最佳预构建库是什么 [英] What are the best prebuilt libraries for doing Web Crawling in Python

查看:25
本文介绍了在 Python 中进行 Web 爬网的最佳预构建库是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在本地抓取和存储有限网站列表的内容,以供将来分析.我基本上想浏览所有页面并跟踪所有内部链接以获取整个公开网站.

I need to crawl and store locally for future analysis the contents of a finite list of websites. I basically want to slurp in all pages and follow all internal links to get the entire publicly available site.

是否有现有的免费图书馆可以让我到达那里?我见过奇尔卡特,但它是为了报酬.我只是在这里寻找基线功能.想法?建议?

Are there existing free libraries to get me there? I've seen Chilkat, but it's for pay. I'm just looking for baseline functionality here. Thoughts? Suggestions?

完全重复:有人知道我可以使用的基于 Python 的优秀网络爬虫吗?

推荐答案

使用 Scrapy.

它是一个基于扭曲的网络爬虫框架.仍在大力开发中,但它已经可以工作了.有很多好东西:

It is a twisted-based web crawler framework. Still under heavy development but it works already. Has many goodies:

  • 内置支持解析 HTML、XML、CSV 和 Javascript
  • 用于抓取带有图像(或任何其他媒体)的项目并下载图像文件的媒体管道
  • 支持通过使用中间件、扩展和管道插入您自己的功能来扩展 Scrapy
  • 广泛的内置中间件和扩展,用于处理压缩、缓存、cookie、身份验证、用户代理欺骗、robots.txt 处理、统计、抓取深度限制等
  • 交互式抓取 shell 控制台,对开发和调试非常有用
  • 用于监控和控制机器人的网络管理控制台
  • 用于对 Scrapy 进程进行低级访问的 Telnet 控制台

使用 XPath 提取有关今天添加到 mininova torrent 站点的所有 torrent 文件的信息的示例代码返回的 HTML 上的选择器:

Example code to extract information about all torrent files added today in the mininova torrent site, by using a XPath selector on the HTML returned:

class Torrent(ScrapedItem):
    pass

class MininovaSpider(CrawlSpider):
    domain_name = 'mininova.org'
    start_urls = ['http://www.mininova.org/today']
    rules = [Rule(RegexLinkExtractor(allow=['/tor/\d+']), 'parse_torrent')]

    def parse_torrent(self, response):
        x = HtmlXPathSelector(response)
        torrent = Torrent()

        torrent.url = response.url
        torrent.name = x.x("//h1/text()").extract()
        torrent.description = x.x("//div[@id='description']").extract()
        torrent.size = x.x("//div[@id='info-left']/p[2]/text()[2]").extract()
        return [torrent]

这篇关于在 Python 中进行 Web 爬网的最佳预构建库是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆