【问题标题】:How to download images from a website that are running in a loop?如何从循环运行的网站下载图像?
【发布时间】:2013-09-14 21:43:51
【问题描述】:

这是网站:

http://www.sat24.com/foreloop.aspx?type=1&continent=europa# 那里的图像循环移动。

这是一张图片的 url 示例:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309171200&cultuur=en-GB&continent=europa 中间有时间日期:201309171200 我需要以某种方式自动解析每个 url 的时间和日期。

例如构建一些字符串:

"www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=" + parsedDateAndTime + &cultuur=en-GB&continent=europa 到目前为止我尝试的是这样的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;

namespace DownloadImages
{
    public partial class Form1 : Form
    {
        int counter;

        public Form1()
        {
            InitializeComponent();

            counter = 0;

            string localFilename = @"d:\localpath\";
            while (true)
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#", localFilename + counter.ToString("D6") + ".jpg");
                    counter++;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

但我还没有解析任何 url,我只是使用主循环 url,我看到它每次都在下载 46kb 文件,但我无法打开它们,我得到一个错误,画图无法打开它......等等

我这样做的方式是错误的。

如何从循环中下载站点中的每个图像?

如何从每张图片中解析日期和时间,这样它就不会一直下​​载同一张图片?我需要以某种方式获取每个图像网址的日期和时间,并将其用作标志或其他东西,这样它就不会下载相同的文件。

编辑**

例如,每个图像的每个 url 的日期和时间都在变化:

http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309161500&cultuur=en-GB&continent=europa

下一个图片网址将是:http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309171500&cultuur=en-GB&continent=europa

日期和时间会根据循环变化,就像在网站中一样,如果您右键单击图像并制作:复制图像URL,您可以看到时间和日期是每张图像的变化。

【问题讨论】:

  • 您无法打开图像,因为在您的代码中您正在下载一个 gif 文件,您的代码提供了 *.jpg 扩展名
  • 除非您改进它,否则两次发布相同的问题对您没有多大帮助。 stackoverflow.com/questions/18805142/…
  • 放更多信息。就像日期时间是随机使用或固定的。并且您此时正在更改扩展名。我以为你必须下载原始图像,它会正常打开
  • 日期和时间在循环中每个 url 都是固定的。
  • 更改了扩展gif的行:localFilename + counter.ToString("D6") + ".gif" 仍然无法打开文件同样的错误。

标签: c# winforms


【解决方案1】:

我假设你的意思是你得到一个表单的 URL:

"http://www.niederschlagsradar.de/images.aspx?
    jaar=-6&type=europa.precip&datum=201309171500&cultuur=en-GB&continent=europa"

您想提取该日期和时间位,以便将其与您已有的图像列表进行比较。所以在上面,你想得到201309171500

您可以使用正则表达式来做到这一点:

string theUrl = @"http://www.niederschlahttp://www.niederschlagsradar.de/images.aspx?
    jaar=-6&type=europa.precip&datum=201309171500&cultuur=en-GB&continent=europa";

Match m = Regex.Match(theUrl, @"&datum=(\d{12})&");
if (m.Success)
{
    string theDate = m.Groups[1].Value;
    Console.WriteLine(theDate);
}

其他信息

如果您查看原始 URL http://www.sat24.com/foreloop.aspx?type=1&continent=europa# 中的 HTML,您会看到一些如下所示的 Javascript:

var images = new Array(
    "http://www.niederschlagsradar.de/images.aspx?jaar=-6&type=europa.precip&datum=201309150000&cultuur=en-GB&continent=europa",
    "http://www.niederschlagsradar.de/images.aspx?
    // many more image URLs here
);

您需要下载 HTML 页面,在 HTML 中找到该数组,然后解析出各个图像的 URL。然后你可以依次下载每张图片。

【讨论】:

  • Jim 是的,但问题是网站上的每次 url 都不同。网站循环中的每个图像的日期和时间都不同。所以我不能把变量 theUrl 固定的 url 与固定的日期和时间。这就是每次获取下一个图片 url 的问题。
  • 网站上有动画:sat24.com/foreloop.aspx?type=1 如果我将鼠标放在图像上,它会自动在动画上单击鼠标右键并复制图像 url,我每次获取不同日期和时间的 url .我需要在循环中保存/下载图像。我不确定它是否可能,但变量 theUrl 应该类似于 "niederschlahttp://www.niederschlagsradar.de/images.aspx? jaar=-6&type=europa.precip&datum=" + datetimevariable + "&cultuur=en-GB&continent=europa"
猜你喜欢
  • 2017-04-27
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多