用于将来自其他网站的内容嵌入(展开)到您自己网站中的库

pyunfurl的Python项目详细描述


Pyunfurl

pyunfurl是一个python库,用于将来自其他站点的内容嵌入到您自己的站点中,类似于slack扩展链接的方式。
对于轻松嵌入tweets、youtube视频、stackoverflow帖子或任何其他网站的内容非常有用。

功能

安装

使用包管理器pip安装pyunfurl。

pip install pyunfurl

用法

importpyunfurlpyunfurl.unfurl('https://www.youtube.com/watch?v=aBr2kKAHN6M')

这将返回与oembed规范类似的dict:

{
	'method': 'oembed', # one of 'oembed', 'twitter_card', 'open_graph', 'meta_tags', 'custom'
	'site': 'YouTube', 
	'domain': 'youtube.com', 
	'title': 'Live Views of Starman', 
	'description': '', 
	'image': '', 
	'favicon': '',  # only included if no image was found 
	'url': 'https://www.youtube.com/watch?v=aBr2kKAHN6M', 
	'type': 'rich', 
	'html': '\n<iframe width=" 480" height="270" src="https://www.youtube.com/embed/aBr2kKAHN6M?feature=oembed" frameborder="0" allowfullscreen="allowfullscreen"></iframe>\n'
}

您可以使用html条目直接包含站点上的内容。

造型

对于非oembed内容(如opengraph条目,如stackoverflow),pyunfurl返回如下html结构:

pyunfurl.unfurl('https://stackoverflow.com/questions/509211/understanding-slice-notation')['html']
<div class="unfurl unfurl-image">
    <a rel="noopener nofollow" target="_blank" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">
        <img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded">
    </a>
    <div class="unfurl-content">
        <a class="unfurl-title" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">Understanding slice notation</a>
        <div>I need a good explanation (references are a plus) on Python's slice notation. 
To me, this notation needs a bit of picking up. 
It looks extremely powerful, but I haven't quite got my head around...</div>
        <a class="unfurl-domain" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">stackoverflow.com</a>
    </div>
</div>

您可以按自己的意愿设计样式,一个好的起点是在您的站点上包含以下css:

 div.unfurl{
        font-family: helvetica, arial, sans-serif;
        font-size:15px;
        border:1px solid #ebebeb;
        border-radius: 8px;
        display:flex;
        align-items: center;
        text-align: left;
    }
    div.unfurl img {
        width: 120px;
        background-color: #ebebeb;
     }
    div.unfurl-favicon img {
        width: 80px;
        padding:20px;
        background-color: #ebebeb;
    }
    div.unfurl-content a{
        color:black;
        display: inline-block;
        font-weight: bold;
        text-decoration: none;
        line-height:30px;
        text-overflow: ellipsis;
        overflow: hidden;
        min-width: 10px;
        white-space: nowrap;
        width:100%;
    }
    div.unfurl-content a.unfurl-domain{
        color:gray;
        font-weight: normal;
    }
    div.unfurl-content{
        flex:1;
        margin-left:10px;
        margin-right:10px;
        min-width: 10px;
        width:100%;
    }
    div.unfurl-content div{
        line-height:18px;
        max-height:54px;
        overflow:hidden;
    }

这将生成一张类似于:

img

贡献

欢迎拉取请求。pyunfurl支持一些不返回任何元标记的站点的自定义集成,如果您想改进特定站点的集成,可以查看hackernews示例。

许可证

MIT

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java希望直接扫描要上传到网页的文档   java JavaFX使对象可见,但不使用(忽略)点击   java LibGdx奇怪的纹理行为   java JavaFx ComboBox在第一次单击时未获得正确的值   api WebDav服务器库最好使用Java   java在Android中维护应用程序状态   java保存在Android中拖放RecyclerView后所做的更改我已经阅读了所有内容   无法读取项目中所需库tomcatembedwebsocket的java存档,或者该存档不是有效的ZIP文件   c#类在系统中有哪些关联?如何在UML中最好地表示它们?   java如何使用bazaar访问两台计算机上的同一文件夹   Java易失性与缓存一致性   java如何解决http11。校长:例外?   java liferaymavenplugin从maven Liferay 6.2.5(6.2 ga6)部署war   安卓的jar for java在哪里。木卫一。文件   java邻接矩阵中BFS的顺序是什么?   虚拟机如何从java启动linux虚拟机?   向DefaultListModel添加元素时java JList未更新   java JMSListener批注无法与REST服务一起使用   java DirectoryStream返回路径的顺序是什么?文件名,上次修改,文件大小?   java DropWizard Hibernate doc想说什么?