阅读包含 Base64 嵌入格式的所有图像的网页 [英] Read a web page with all images in Base64-Embedded format

查看:21
本文介绍了阅读包含 Base64 嵌入格式的所有图像的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的场景中,我想以编程方式下载页面(互联网上的任何页面)的 HTML,但我也希望 HTML 中的所有图像都采用 base64 嵌入格式(未引用)

In my scenario I want to download the HTML of a page (any page on the Internet) programaticaly but also I want all of the images in the HTML to be in base64 embedded format (not referenced)

换句话说,不是:

<img src='/images/delete.gif' />

我希望下载的 html 看起来像这样:

I want the downloaded html to look like this:

<img src="data:image/gif;base64,R0lGODl..." />

这样我就不需要经历将所有图像存储在目录等中的过程.

This way I don't need to go through the process of storing all images in directories, etc, etc.

你们中有人知道如何做到这一点吗?或者有什么插件可以有效地做到这一点?

Does any of you have any idea how this can be done? Or any plugin to do this efficiently?

推荐答案

嗯,你需要:

  • 下载原始 HTML
  • 在 HTML 中查找每个 img 元素(例如使用 HTML 敏捷包) 和每个:
    • 如果它已经在使用数据 URL,请忽略它
    • 否则:
    • 下载图片
    • 使用 Convert.ToBase64String 以 Base64 编码它
    • 将原始 img 标签替换为使用 base64 版本的标签(在原始字符串中或通过 DOM 表示)
    • Download the original HTML
    • Find each img element in the HTML (for instance using the HTML agility pack) and for each one:
      • If it's already using a data URL, ignore it
      • Otherwise:
      • Download the image
      • Encoded it in Base64 using Convert.ToBase64String
      • Replace the original img tag with one using the base64 version (either in the original string, or via a DOM representation)

      这些步骤中的任何一个是否会导致您遇到特定问题?您可以通过并行下载图像来加快速度,但我会先获得串行版本.

      Is any of these steps causing you a particular problem? You could potentially make it quicker by downloading the images in parallel, but I'd get a serial version working first.

      这篇关于阅读包含 Base64 嵌入格式的所有图像的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆