我試圖來用LinkedIn提取一些數據網格,我只是試圖讓這對我自己的學習曲線的工作,但如果我刪除行瀏覽器加載文件
MessageBox.Show("asdfasdfasdf")
名單「消息「只有1個項目,如果我包含上面的行確實是預期的,我會得到15條消息
有人可以解釋嗎?
public void extract_messages_received(object sender, RoutedEventArgs e)
{
triggered = false;
System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
browser.Navigate(new Uri(@"http://www.linkedin.com/inbox/messages/received"));
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
}
private void LoadMessages(string url)
{
txtOutput.Text = @"http://www.linkedin.com" + url.Substring(6, url.Length - 6);
if (!urls.Contains(url))
{
urls.Add(url);
WebBrowser browser = new WebBrowser();
browser.Navigate(new Uri(txtOutput.Text);
loaded_message = false;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(ReadMessages);
}
}
private void ReadMessages(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (loaded_message == false)
{
string url = ((WebBrowser)sender).Url.ToString();
int loc1 = url.IndexOf("itemID") + 7;
int loc2 = url.IndexOf("&", loc1);
IEnumerable<string> name = null;
IEnumerable<string> odate = null;
IEnumerable<string> photo = null;
IEnumerable<string> subject = null;
IEnumerable<string> headline = null;
string body = "";
string id = url.Substring(loc1, loc2 - loc1);
//System.Windows.MessageBox.Show("READ");
foreach (HtmlElement element in ((WebBrowser)sender).Document.GetElementsByTagName("div"))
{
if (element.GetAttribute("classname").Equals("inbox-item-body"))
{
body = element.InnerText;
}
if (element.GetAttribute("classname").Equals("inbox-item-header"))
{
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(element.InnerHtml);
name = from foo in doc.DocumentNode.SelectNodes("//a[@class='fn']") select foo.InnerText;
odate = from foo in doc.DocumentNode.SelectNodes("//p[@class='date']") select foo.InnerText;
photo = from foo in doc.DocumentNode.SelectNodes("//img[@class='photo']") select foo.Attributes["src"].Value;
subject = from foo in doc.DocumentNode.SelectNodes("//h3") select foo.InnerText;
headline = from foo in doc.DocumentNode.SelectNodes("//span[@class='headline']") select foo.InnerText;
}
}
// ****
MessageBox.Show("asdfasdfasdf");
// ****
messages.Add(new Messages()
{
ID = id,
Subject = subject.First().ToString(),
Headline = headline.First().ToString(),
Sender = name.First().ToString(),
Photo = photo.First().ToString(),
SendDate = odate.First().ToString(),
Body = body
});
// dataMessages.ItemsSource = messages;
}
loaded_message = true;
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!triggered)
{
triggered = true;
System.Windows.Forms.WebBrowser web = sender as System.Windows.Forms.WebBrowser;
foreach (HtmlElement element in web.Document.GetElementsByTagName("ol"))
{
if (element.GetAttribute("classname").Contains("inbox-list "))
{
WebBrowser browser = new WebBrowser();
browser.Navigate("about:blank");
browser.Document.Write(element.InnerHtml);
HtmlElementCollection hrefTags = null;
hrefTags = browser.Document.GetElementsByTagName("a");
foreach (HtmlElement a in hrefTags)
{
if (a.OuterHtml.Contains("displayMBox"))
{
LoadMessages(a.GetAttribute("href"));
}
}
}
}
}
}
不錯的約翰!答對了 ;-) – user1320651