0

我在尝试从 Web 文档中提取超链接时遇到问题!

我尝试使用的方法如下所示:

HtmlElementCollection ht = wb.Document.Links;

foreach (HtmlElement item in ht)
{
    if (item.GetAttribute("href").Contains("name"))
    {
        linkList.Add(item.GetAttribute("href"));
    }
}

执行此代码时,我收到错误“指定的演员表无效”。我想问题出在事实上,执行此代码的方法是在与 webbrowser 不同的线程上调用的。在同一个线程上,我调用该方法没有问题。

4

2 回答 2

1

你可以试试这段代码

        HtmlElementCollection hc = webBrowser1.Document.GetElementsByTagName("a");
        for (int i = 0; i < hc.Count; i++)
        {
            if (hc[i].GetAttribute("href") == "name")
                listBox1.Items.Add(hc[i].InnerHtml);// Or InnerText
        }
于 2013-04-30T20:56:43.667 回答
1

我找到的解决方案是将“链接获取代码”放在单独的方法中并在主线程(运行浏览器的位置)上调用该方法。

BeginInvoke(new MethodInvoker(delegate() { getUsers(webBrowser1, linkList); }));

于 2013-05-02T18:15:16.283 回答