基本网页爬虫,自动化网站探索并生成网页资源树。
项目描述
半自动爬虫,具有自动提取网站结构的特殊功能。
from basiccrawler.crawler import BasicCrawler SOURCE_DOMAIN='http://learningequality.org' start_page = 'http://learningequality.org/kolibri/' class LECrawler(BasicCrawler): pass crawler = LECrawler(main_source_domain=SOURCE_DOMAIN, start_page=start_page) web_resource_tree = crawler.crawl()
爬虫将总结发现结果(根据粗略的启发式算法)。
# CRAWLER RECOMMENDATIONS BASED ON URLS ENCOUNTERED: ################################################################################ 1. These URLs are very common and look like global navigation links: - http://learningequality.org/about/team/ - http://learningequality.org/about/board/ - http://learningequality.org/about/supporters/ - ... 2. These are common path fragments found in URLs paths, so could correspond to site struture: - ... ################################################################################
网页资源树包含有关网站结构的高层次信息(print_depth=3)或全部详细信息(print_depth=100)。例如
crawler.print_tree(web_resource_tree, print_depth=4) - path: /kolibri/ (PageWebResource) children: - path: / (PageWebResource) children: - path: /media/Rapport-Etude-Cameroun_KL_ENG.pdf (MediaWebResource) - path: /about/ (PageWebResource) children: - path: /ka-lite/map/ (PageWebResource) - path: /about/values/ (PageWebResource) - path: /about/team/ (PageWebResource) - path: /about/board/ (PageWebResource) - path: /about/supporters/ (PageWebResource) - path: /about/press/ (PageWebResource) - path: /about/jobs/ (PageWebResource) - path: /about/internships/ (PageWebResource) children: - path: https://learningequality.org/about/jobs/?gh_jid=533166 (PageWebResource) - path: /download/ (PageWebResource) - path: /documentation/ (PageWebResource) - path: /hardware_grant/ (PageWebResource) - path: /ka-lite/ (PageWebResource) children: - path: /ka-lite/infographic/ (PageWebResource) - path: /translate/ (PageWebResource) - path: https://blog.learningequality.org/?gi=2589e076ea04 (PageWebResource) - path: /ka-lite/map/add/ (PageWebResource) - path: /donate/ (PageWebResource) children: - path: /static/doc/learning_equality_irs_determination_letter.pdf (MediaWebResource) - path: /cdn-cgi/l/email-protection (PageWebResource)
对于这次爬取,我们没有找到太多教育材料(文档/视频/音频/网络应用程序),但至少我们对该页面的链接有了些了解。尝试在另一个网站上使用。
示例用法
https://github.com/learningequality/sushi-chef-tessa/blob/master/tessa_cralwer.py#L229
待办事项
更新示例+笔记本
URL路径 / 反向路径(可能还有其他地方):考虑urllib.urlparse? [例如 url.startwith(source_domain) 可以是 source_domain in url.domain 以使其对子域更灵活
可以指定其他有效域名,但url_to_path_list假定添加
[我们可能希望根据父URL扩展所有链接] 重构并删除对MAIN_SOURCE_DOMAIN的需求,而只使用SOURCE_DOMAINS
未来功能想法
异步下载(不是必需的,但对于大型站点可能有助于性能)
不阻塞HTTP请求
允许多个工作线程从队列中获取任务
content_selector提示用于默认on_page处理程序,仅跟踪HTML树中特定子集内的链接。可以具有
网站级选择器在类级别
通过上下文字典传递从引用页面提供的额外content_selector
自动检测标准嵌入标签(音频、视频、PDF等)并在默认on_page处理程序中添加到网络资源树。
爬虫API
BasicCrawler类的目标是帮助进行源网站的初始探索。编写一个子类,使用HTML、URL结构和内容来引导爬取并生成网络资源树是您的责任。
您的爬虫应继承自BasicCrawler并定义
我们正在爬取哪个网站以及从哪里开始
设置以下属性
MAIN_SOURCE_DOMAIN,例如 'https://learningequality.org' 或在创建时作为参数传递 main_source_domain。
START_PAGE,例如 'https://learningequality.org/' 或在创建时传递 start_page。
IGNORE_URLS=[]:爬虫将忽略这些URL(可以是字符串、正则表达式或可调用对象)
CRAWLING_STAGE_OUTPUT='chefdata/trees/web_resource_tree.json':爬取输出的存储位置
首次运行时,通过调用 crawler.crawl() 或作为命令行脚本运行
BasicCrawler包含访问页面的逻辑,并将根据在初始爬取期间观察到的URL结构打印出自动推断的网站结构发现和建议。
根据链接在不同页面上的出现次数,爬虫将向您建议全局导航链接的候选者。大多数网站都有一个/about页面、/contact us等非内容页面,我们不想将这些页面包含在网络资源树中。您应该检查这些建议并决定哪些应该被忽略(即不爬取或包含在web_resource_tree输出中)。要忽略URL,可以编辑属性
IGNORE_URLS:爬虫将忽略这些URL。编辑您的爬虫子类的代码,并将您想跳过的URL(任何不太可能包含内容的东西)追加到IGNORE_URLS中。
再次运行爬虫,这次输出中应该噪音更少。
注意可能需要特殊处理的路径的建议(例如/course、/lesson、/content等)。您可以定义类方法来处理每种URL类型
def on_course(self, url, page, context): # what do you want the crawler to do when it visits the course with `url` # in the `context` (used for extra metadata; contains reference to parent) # The BeautifulSoup parsed contents of the `url` are provided as `page`. def on_lesson(self, url, page, context): # what do you want the crawler to do when it visits the lesson def on_content(self, url, page, context): # what do you want the crawler to do when it visits the content url
查看默认的on_page方法,以了解网络资源树是如何构建的:https://github.com/learningequality/BasicCrawler/blob/master/basiccrawler/crawler.py#L212