我正在寻找这样的东西:
data = '''
**this is some markdown**
blah blah blah


'''
print get_images_url_from_markdown(data)
Run Code Online (Sandbox Code Playgroud)
返回文本中的图像URL列表:
['http://somewebsite.com/image1.jpg', 'http://anotherwebsite.com/image2.jpg']
Run Code Online (Sandbox Code Playgroud)
有什么可用的,或者我是否必须使用BeautifulSoup刮掉Markdown?
Way*_*lan 13
Python-Markdown有一个广泛的扩展API.事实上,目录扩展基本上用你想要的标题(而不是图像)加上你不需要的一堆其他东西(比如添加唯一的id属性和为TOC构建嵌套列表).
解析文档后,它包含在ElementTree对象中,您可以使用树处理器在将树序列化为文本之前提取所需的数据.请注意,如果您将任何图像作为原始HTML包含在内,则无法找到这些图像(在这种情况下,您需要解析HTML输出并提取).
从本教程开始,除了您需要创建一个treeprocessor而不是内联Pattern.你应该得到这样的东西:
import markdown
from markdown.treeprocessors import Treeprocessor
from markdown.extensions import Extension
# First create the treeprocessor
class ImgExtractor(Treeprocessor):
def run(self, doc):
"Find all images and append to markdown.images. "
self.markdown.images = []
for image in doc.findall('.//img'):
self.markdown.images.append(image.get('src'))
# Then tell markdown about it
class ImgExtExtension(Extension):
def extendMarkdown(self, md, md_globals):
img_ext = ImgExtractor(md)
md.treeprocessors.add('imgext', img_ext, '>inline')
# Finally create an instance of the Markdown class with the new extension
md = markdown.Markdown(extensions=[ImgExtExtension()])
# Now let's test it out:
data = '''
**this is some markdown**
blah blah blah


'''
html = md.convert(data)
print md.images
Run Code Online (Sandbox Code Playgroud)
以上输出:
[u'http://somewebsite.com/image1.jpg', u'http://anotherwebsite.com/image2.jpg']
Run Code Online (Sandbox Code Playgroud)
如果你真的想要一个返回列表的函数,只需将它们全部包装在一起,你就可以了.
| 归档时间: |
|
| 查看次数: |
1201 次 |
| 最近记录: |