简体   繁体   English

SVG从链接下载到文件失败,因为无效的base64?

[英]Downloads SVG from link to file fails as not valid base64?

So I'm trying to download an SVG image it looks like base64 but fails. 因此,我正在尝试下载看起来像base64但失败的SVG图像。 The URL is a lot longer than this but I shortened it for veiwers sake. 该URL的长度比此长度长很多,但为方便起见,我将其缩短了。

data:image/svg+xml;utf8,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%0A%3Csvg%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20height%3D%2232%22%20version%3D%221.1%22%20viewBox%3D%220%200%20320%20320%22%20wid3A%20%20%3Crect%20height%3D%22320%22%20id%3D%22rect%22%20rx%3D%2251.2%22%20width%3D%22320%22%20x%3D%220%22%20y%3D%220%22%2F%3E%0A%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%3Cuse%20xlink%3Ahref%3D%22%23rect%22%2F%3E%0A%20%20%20%20%3C%2FclipPath%3E%0A%20%20%3C%2Fdefs%3E%0A%20%20%3Cuse%20fill%3D%22%23FFFC00%22%20stroke%3D%22black%22%20stroke- 数据:image / svg + xml; utf8,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20standalone%3D%22no%22%3F%3E%0A%3Csvg%20xmlns %3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20height%3D%2232%22%20version%3D%221.1%22%20viewBox%3D%220%200%20320%20320 %22%20wid3A%20%20%3Crect%20height%3D%22320%22%20id%3D%22rect%22%20rx%3D%2251.2%22%20width%3D%22320%22%20x%3D%220%22 %20y%3D%220%22%2F%3E%0A%20%20%20%20%3CclipPath%20id%3D%22clip%22%3E%0A%20%20%20%20%20%20%20%3Cuse %20xlink%3Ahref%3D%22%23rect%22%2F%3E%0A%20%20%20%20%3C%2FclipPath%3E%0A%20%20%20%3C%2Fdefs%3E%0A%20%20 %3Cuse%20fill%3D%22%23FFFC00%22%20stroke%3D%22black%22%20stroke-

Code: 码:

foreach (var username in File.ReadAllLines("/Users/admin/Desktop/snap-scraper/snap-scraper/snapchats.txt"))
{
    Console.WriteLine($"Attempting to grab QR for {username}");

    driver.Navigate().GoToUrl($"https://snapchat.com/add/{username}");

    Thread.Sleep(1000);

    var htmlDocument = new HtmlDocument();
    htmlDocument.LoadHtml(driver.PageSource);

    var image = htmlDocument.DocumentNode.SelectSingleNode("//img");

    if (image == null || !image.Attributes.Contains("src"))
    {
        Console.WriteLine($"Something went wrong for {username}");
        continue;
    }

    Console.WriteLine($"Got the QR for {username} yay");

    var src = image.Attributes.Where(x => x.Name == "src").First().Value;

    string filePath = $"/Users/admin/Desktop/snap-scraper/snap-scraper/images/{username}.jpg";
    File.WriteAllBytes(filePath, Convert.FromBase64String(src.Replace("-", "")));
}

It's not Base64 . 不是Base64 It's URL Encoding . 这是URL Encoding You can see this in a URL Decoder Tool if you paste in the encoded URL. 如果粘贴已编码的URL,则可以在URL解码器工具中看到此内容。

Since SVG's are made up of an XML format they do not require encoding. 由于SVG由XML format因此不需要编码。

You can use WebUtility.UrlDecode(String) to decode the string and then convert it to bytes using Encoding.GetBytes(String) write the bytes to disk. 您可以使用WebUtility.UrlDecode(String)解码字符串,然后使用Encoding.GetBytes(String)将其转换为字节,然后将字节写入磁盘。

Example: 例:

foreach (var username in File.ReadAllLines("/Users/admin/Desktop/snap-scraper/snap-scraper/snapchats.txt"))
{
    Console.WriteLine($"Attempting to grab QR for {username}");

    driver.Navigate().GoToUrl($"https://snapchat.com/add/{username}");

    Thread.Sleep(1000);

    var htmlDocument = new HtmlDocument();
    htmlDocument.LoadHtml(driver.PageSource);

    var image = htmlDocument.DocumentNode.SelectSingleNode("//img");

    if (image == null || !image.Attributes.Contains("src"))
    {
        Console.WriteLine($"Something went wrong for {username}");
        continue;
    }

    Console.WriteLine($"Got the QR for {username} yay");

    var src = image.Attributes.Where(x => x.Name == "src").First().Value;

    string filePath = $"/Users/admin/Desktop/snap-scraper/snap-scraper/images/{username}.svg"; // This was .jpg

    // URL Decode Image - Remember to strip the start of the data url e.g. data:image/svg xml;utf8,
    string svg = WebUtility.UrlDecode(src).replace("data:image/svg xml;utf8,", "");

    // Convert SVG to byte array
    byte[] svgBytes = Encoding.UTF8.GetBytes(decodedUrl);

    // Write to byte array to disk
    File.WriteAllBytes(filePath, svg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM