从9gag.com获取资料的python库
python-ninegag的Python项目详细描述
从9gag.com获取资料的python库
安装
通过PIP:
pip install python-ninegag
用法
pyninegag.get_sections()
返回9gag节的dict,其中键是大写的节名称,值是它们的url。
示例输出:
{'Fresh':'http://9gag.com/fresh','Cute Animals':'http://9gag.com/cute','Comic':'http://9gag.com/comic','Food':'http://9gag.com/food','GIF':'http://9gag.com/gif','Cosplay':'http://9gag.com/cosplay','Geeky':'http://9gag.com/geeky','Girl':'http://9gag.com/girl','Funny':'http://9gag.com/funny','Design':'http://9gag.com/design','WTF':'http://9gag.com/wtf','NSFW':'http://9gag.com/nsfw','Trending':'http://9gag.com/trending','Timely':'http://9gag.com/timely','Meme':'http://9gag.com/meme','Hot':'http://9gag.com/hot'}
注意:nsfw部分将返回零个项目(空iterable),因为观看此部分需要用户登录,而此库不支持登录。
这些值不是硬编码的,将在第一次请求时生成。值被缓存。
pyninegag.get_articles(url,max_pages=1)
返回iterable,其中包含在给定url上找到的所有文章。 max_pages-要分析多少页结果。如果没有-全部可用。默认1-仅第一页。
pyninegag.get_articles(pyninegag.BASE_URL)
将返回主9gag页上的所有文章。
示例输出:
[{'id':'a1MEzz6','title':'Jumping into the abyss','data':{'type':'gif','url':'http://img-9gag-fun.9cache.com/photo/a1MEzz6_460sa.gif'},'comments':'163','votes':'10709','url':'http://9gag.com/gag/a1MEzz6'},{'id':'agVEP3g','title':'Made of crab','data':{'type':'image','url':'http://img-9gag-fun.9cache.com/photo/agVEP3g_700b_v1.jpg'},'comments':'257','votes':'10053','url':'http://9gag.com/gag/agVEP3g'}]
文章类型可以是gif、image或longpost之一。长柱只是一个很高的形象。
pyninegag.get_by_section(section_name,max_pages=1)
返回iterable和给定部分中找到的所有文章。节名称必须是pyninegag.get_sections()的键之一。如果找不到,ValueError将被筹集。
还启用了日志记录器pyninegag,记录解析错误的异常和警告。所有错误都在消息中包含项目ID和URL,以便于调试问题。