用于将来自其他网站的内容嵌入(展开)到您自己网站中的库
pyunfurl的Python项目详细描述
Pyunfurl
pyunfurl是一个python库,用于将来自其他站点的内容嵌入到您自己的站点中,类似于slack扩展链接的方式。
对于轻松嵌入tweets、youtube视频、stackoverflow帖子或任何其他网站的内容非常有用。
功能
- 默认支持来自https://oembed.com/和https://noembed.com/的所有oEmbed提供程序。
- 支持oembed规范的autodiscovery部分。
- 支持Open Graph协议。
- 支持Twitter Cards
- 如果所有其他操作都失败,则返回到meta标记和站点favicon/title。
安装
使用包管理器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;
}
这将生成一张类似于:
贡献
欢迎拉取请求。pyunfurl支持一些不返回任何元标记的站点的自定义集成,如果您想改进特定站点的集成,可以查看hackernews示例。