【发布时间】:2015-09-01 11:30:22
【问题描述】:
我有问题,我编写了获取捷克电台当前歌曲的方法。他们没有 API,所以我不得不通过 html agility.dll 从 html 获取歌曲 问题是即使页面上的歌曲标题更改,我的方法下载旧页面,通常我必须等待 20 秒并关闭我的应用程序,然后它才能工作。 我认为一些缓存问题,但我无法解决它。 试过:DownloadString 方法也没有刷新。
public static string[] GetEV2Songs()
{
List<string> songy = new List<string>();
string urlAddress = "http://www.evropa2.cz/";
string data = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
readStream = new StreamReader(receiveStream);
else
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
data = readStream.ReadToEnd();
response.Close();
readStream.Close();
}
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(data);
string temp = "";
foreach (var node in doc.DocumentNode.SelectNodes("//body//h2"))
{
if (node.InnerText.Contains("&ndash"))
{
temp = node.InnerText.Replace("–", "-");
songy.Add(temp);
}
}
return songy.ToArray();
}
【问题讨论】:
-
问题可能出在调用此方法的代码中。例如,如果此方法抛出异常或返回一个空数组,那么调用代码如何处理呢?是否有某种变量保存在内存中的某个地方,如果此方法失败,可能不会更新?此外,如果您使用的是 ASP.NET,则值得检查显示结果的页面是否未缓存结果。
-
你怎么知道它是“旧”页面?歌曲的播放时间通常超过 20 秒,并且页面包含一个轮播,其中包含三首歌曲。它只能每 60 秒刷新一次
-
另外,“live”歌曲出现在
h4标签中,你是不是看错标签了? -
不,获取当前歌曲文本的方法有效,问题是我什至检查了页面的源代码,并且在 h4 元素中更改了现场歌曲,但是我的方法下载带有旧 h4 元素的页面老歌。
-
Panagiotis Kanavos!你是对的,收音机每 60 秒刷新一次,当前歌曲、上一首歌曲和上一首歌曲。只有当歌曲之间有广告时,它才会立即切换!非常感谢!你能解释一下为什么在浏览器上我可以看到它发生了变化,但应用程序只有在 60 秒后才能看到? :)
标签: c# html-agility-pack system.net