나는이 페이지를 가지고있다 : www.unnu.com/music-artists
모든 아티스트와 링크의 전체 목록을 검색해야합니다.
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
// There are various options, set as needed
htmlDoc.OptionFixNestedTags = true;
// filePath is a path to a file containing the html
htmlDoc.Load("http://www.unnu.com/music-artists");
// Use: htmlDoc.LoadHtml(xmlString); to load from a string (was htmlDoc.LoadXML(xmlString)
// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
{
// Handle any parse errors as required
}
else
{
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//a");
if (bodyNode != null)
{
listBox1 = ????//this is not working, do not know if the code is correct and just missing here or is this all wrong.
}
}
}
제발, 나는 일이 긴급함과 함께 이것을 필요로합니다. 내 스카 이프가 필요한 경우 gnrmalazagnr을 추가 할 수 있습니다.
나는 당신이 여기서 무엇을하고 싶은지 모르지만 html 파일을 다운로드하고 읽고 싶을 뿐이다. WebClient web = new WebClient(); web.DownloadStringAsync(new Uri("www.unnu.com/music-artists")); web.DownloadStringCompleted += web_DownloadStringCompleted;
가 필요합니다 WebClient web = new WebClient(); web.DownloadStringAsync(new Uri("www.unnu.com/music-artists")); web.DownloadStringCompleted += web_DownloadStringCompleted;
void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { string HtmlPage = e.Result; }
나는이 문제를 해결한다.
void web_DownloadStringCompleted (객체 발신자, DownloadStringCompletedEventArgs) {
if (!e.Cancelled && e.Error == null && !String.IsNullOrEmpty(e.Result))
{
_artistas = new List<Artista>();
// Aqui você pega todos os links da página
// P.S.: Se a página mudar, você tem que alterar o pattern aqui.
string pattern = @"\<a\shref\=[\""|\'](?<url>[^\""|\']+)[\""|\']\stitle\=[\""|\'](?<title>[^\""|\']+)[\""|\']\>(?<author>[^\<]+)\<\/a\>";
// Busca no HTML todos os links
MatchCollection ms = Regex.Matches(e.Result, pattern, RegexOptions.Multiline);
Debug.WriteLine("----- OK {0} links encontrados", ms.Count);
foreach (Match m in ms)
{
// O pattern acima está dizendo onde fica o Url e onde fica o nome do artista
// e esses são resgatados aqui
Group url = m.Groups["url"];
Group author = m.Groups["author"];
if (url != null && author != null)
{
//Debug.WriteLine("author: {0}\nUrl: {1}", author.Value, url.Value);
// Se caso tenha encontrado o link do artista (pois há outros links na página) continua
if(url.Value.ToLower().IndexOf("/artist/") > -1)
{
// Adiciona um objeto Artista à lista
Artista artista = new Artista(author.Value, url.Value);
_artistas.Add(artista);
}
}
}
listBox1.ItemsSource = _artistas;
}
}