C# WebClient只下载源页面一次帮助请C

C# WebClient只下载源页面一次帮助请C,c#,C#,我想下载Html源代码并从中提取内容,但webclient只下载一次, 第二次它不工作,直到我退出程序并重新启动它 这是下载功能,我在一个按钮中调用了它: public static async Task DownF(string[] Urls) { WebClient KeyClient = new WebClient(); try { await Task.Run

我想下载Html源代码并从中提取内容,但webclient只下载一次, 第二次它不工作,直到我退出程序并重新启动它

这是下载功能,我在一个按钮中调用了它:

public static async Task DownF(string[] Urls)
        {

            WebClient KeyClient = new WebClient();




            try
            {
                await Task.Run(() =>
                {

                    const string pattern = "<span.*?>(.*?)<\\/span>";


                    for (int i = 0; i < 3; i++)
                    {
                        while (KeyClient.IsBusy)
                        {
                            System.Threading.Thread.Sleep(1000);
                        }

                        string page = KeyClient.DownloadString(Urls[i]);

                        MatchCollection matchs = Regex.Matches(page, pattern);
                        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                        if (matchs.Count > 0)
                        {
                            StreamWriter wrt = new StreamWriter(path + "\\results.txt");


                            int KeyWordCounter = 0;
                            foreach (Match m in matchs)
                            {
                                KeyWordCounter += 1;

                                wrt.WriteLine(KeyWordCounter + "-" + m.Groups[1].Value);

                            }
                            wrt.Close();
                        }

                    }

                    MessageBox.Show("finich!");
                });
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message);

            }

我同意D·福利的观点。您可以通过NuGet安装Selenium.WebDriver,它将为您提供所需的所有功能。它是一个非常酷的软件包,允许您通过多种方式从浏览器中提取数据。它有一些类似于getByXPath、GetByCsSelector等的功能。此外,它还为您提供了一个快速而简单的机会,让您可以非常轻松地浏览网站

更新:

如果我们假设每次重新加载网页时,您尝试获取的元素都位于同一位置,则可以在Chrome浏览器中打开该网页,右键单击需要获取XPATH的元素,然后按照屏幕截图进行操作:


您混合了异步和同步调用,可能会导致死锁。您也没有处理StreamWriter,这可能是一个问题

请尝试以下操作:

WebClient KeyClient=新的WebClient; 尝试 { 常量字符串模式=*?; 对于int i=0;i<3;i++ { string page=wait KeyClient.downloadstringasyncURL[i]; MatchCollection matchs=Regex.Matchespage,pattern; 字符串路径=Environment.GetFolderPathEnvironment.SpecialFolder.Desktop; 如果匹配。计数>0 { 使用StreamWriter wrt=new StreamWriterpath+\\results.txt { int关键字计数器=0; foreach Match m in Match { 关键词计数器+=1; 等待wrt.WriteLineAsyncKeyWordCounter+-+m.Groups[1]。值; } } } } MessageBox.Showfinich!; } 卡奇 { MessageBox.Showe.Message; }
将System.Threading.Thread.Sleep1000替换为Task.DelayError:没有重载方法Delay takes 0 Arguments是的,您需要传递1000相同的问题任务。Delay not workingTask.DelaySystem.TimeSpan.FromSeconds1000;不工作相同的问题嗨,如何使用html agility在两个特殊的html标记之间查找文本,我正在尝试使用html agility c提取网页中和之间的所有文本,但是我不知道xpath是什么:科学是一个系统的企业,它以可测试的解释和对宇宙的预测的形式构建和组织知识嗨,如果我理解正确,那么你可以看看更新的答案。您可以右键单击网页上的任何元素,然后转到“检查”。然后再次右键单击,然后转到复制并获取所需的路径。嗨,如何使用html agility在两个特殊html标记之间查找文本,我正在尝试使用html agility c提取网页中和之间的所有文本,但是我不知道xpath是什么:Science是一个系统的企业,它以可测试的解释和对宇宙的预测的形式构建和组织知识。你能告诉我如何为这个html标记编写xpath吗?
string site1 = "www.site1.com";
                string site2 = "www.site2.com";
                string site3 = "www.site3.com";

                string [] Urls = new string[3];
                Urls[0] = site1;
                Urls[1] = site2;
                Urls[2] = site3;
                 DownF(Urls);