使用c#提取Web文档

时间:2009-10-31 14:08:37

标签: c# browser webpage dom

我正在尝试使用c#

从网页获取数据

到目前为止,这是我的代码:

WebBrowser wb = new WebBrowser();

wb.Url = new Uri("http://www.microsoft.com");
HtmlDocument doc = wb.Document;

MessageBox.Show(doc.ToString());

不幸的是,wb仍为null,并且Url属性永远不会被设置。

有人可以帮我吗?

谢谢

4 个答案:

答案 0 :(得分:5)

我会使用WebClient类而不是Web浏览器。 Web浏览器类更多用于与UI交互,而WebClient更适合与页面进行编程交互。以下是一些示例代码:

private void sendMessage(JaxtrSmsMessage message)
{
    HttpWebRequest request;
    HttpWebResponse response;
    CookieContainer cookies;
    string url = "http://www.jaxtr.com/user/login.jsp";

    try
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = true;
        request.CookieContainer = new CookieContainer();
        response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            StringBuilder sb = new StringBuilder();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            while (!reader.EndOfStream)
            {
                sb.AppendLine(reader.ReadLine());
            }

            //Get the hidden value out of the form.                
            String fp = Regex.Match(sb.ToString(), "\"__fp\"\\svalue=\"(([A-Za-z0-9+/=]){4}){1,19}\"", RegexOptions.None).Value;
            fp = fp.Substring(14);
            fp = fp.Replace("\"", String.Empty);


            cookies = request.CookieContainer;
            //response.Close();
            String requestString = "http://www.jaxtr.com/user/Login.action?tzOffset=6&navigateURL=&refPage=&jaxtrId=" + HttpUtility.UrlEncode(credentials.Username) + "&password=" + HttpUtility.UrlEncode(credentials.Password) + "&Login=Login&_sourcePage=%2Flogin.jsp&__fp="+HttpUtility.UrlEncode(fp);
            request = (HttpWebRequest)WebRequest.Create(requestString);
            request.CookieContainer = cookies; //added by myself

            response = (HttpWebResponse)request.GetResponse();
            Console.WriteLine("Response from login:" + response.StatusCode);

            String messageText = (message.TruncateMessage && message.MessageText.Length > JaxtrSmsMessage.MAX_MESSAGE_LENGTH ? message.MessageText.Substring(JaxtrSmsMessage.MAX_MESSAGE_LENGTH) : message.MessageText);

            String messageURL = "http://www.jaxtr.com/user/sendsms?CountryName=" + HttpUtility.UrlEncode(message.CountryName) + "&phone=" + HttpUtility.UrlEncode(message.DestinationPhoneNumber) + "&message=" + HttpUtility.UrlEncode(messageText) + "&bySMS=" + HttpUtility.UrlEncode(message.BySMS.ToString().ToLower());

            request = (HttpWebRequest)WebRequest.Create(messageURL);
            request.CookieContainer = cookies;
            response = (HttpWebResponse)request.GetResponse();

            Console.WriteLine("Response from send SMS command=" + response.StatusCode);

            StringBuilder output = new StringBuilder();

            using (Stream s = response.GetResponseStream())
            {
                StreamReader sr = new StreamReader(s);
                while (!sr.EndOfStream)
                {
                    output.AppendLine(sr.ReadLine());
                }
            }
            response.Close();
        }
        else
        {
            Console.WriteLine("Client was unable to connect!");
        }
    }
    catch (System.Exception e)
    {
        throw new SMSDeliveryException("Unable to deliver SMS message because "+e.Message, e);
    }
}

答案 1 :(得分:1)

首先,您必须使用URL路径调用navigate方法,并且必须从Web浏览器控件中捕获Document complete事件

 webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
 webBrowser.Navigate("http:\\www.microsoft.com");

然后使用文档完成事件来获取加载的网页文档

 void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {             
          MessageBox.Show(webBrowser.DocumentText.ToString());
    }

希望这会有所帮助......

答案 2 :(得分:0)

我认为您忘记启动导航到该页面。见here

但也......

导航是一个异步过程。您需要将Web浏览器对象放在类范围内,然后处理导航事件,如下所示:

    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        HtmlDocument doc = webBrowser1.Document;
        doc = webBrowser1.Document;
    }

您会在处理程序中看到doc将为非null。

答案 3 :(得分:-1)

尝试DocumentText的{​​{1}}属性,而不是调用HtmlDocument