确定。所以我在网上发现这个代码一切正在进行,但它向我显示了我正在搜索的div类但删除了所有文本。知道为什么吗?下面是其输出内容的一个例子......
<div class="marquee"><img src="logo.png" /></div>
<div id="joke">
<div id="setup" class="exit-left"></div>
<div id="punchline">
<div class="question"></div>
<div id="zing" class="exit-right"></div>
</div>
</div>
<div id="buttons">
<input id="tell-me" class="exit-bottom no-select" type="button" value="Tell Me!" />
<!--<input id="another" class="exit-bottom" type="button" value="Another!" />-->
<table class="another exit-bottom no-select">
<tr>
<td class="another" colspan="3">Another</td>
<td class="share"><div class="share-img"></div>Share</td>
</tr>
</table>
</div>
根本没有显示innertext ... 这是我的代码是VS。
var doc = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlNode.ElementsFlags [&#34; br&#34;] = HtmlAgilityPack.HtmlElementFlag.Empty; doc.OptionWriteEmptyNodes = true;
try
{
var webRequest = HttpWebRequest.Create("http://dadjokegenerator.com/");
Stream stream = webRequest.GetResponse().GetResponseStream();
doc.Load(stream);
stream.Close();
}
catch (System.UriFormatException uex)
{
throw;
}
catch (System.Net.WebException wex)
{
throw;
}
//get the div by id and then get the inner text
doc.GetElementbyId("content").InnerHtml;
await e.Channel.SendMessage("test " + divString); `
答案 0 :(得分:1)
虽然您的代码正确下载了页面http://dadjokegenerator.com/的内容,但InnerHtml是空的,因为此页面实际上不包含您正在寻找的笑话(您可以看到,如果您在Web中显示页面的源代码浏览器 - 例如在Firefox中按CTRL + U)。笑话稍后通过javascript添加到此页面。如果您在http://dadjokegenerator.com/js/main.js查看此Javascript的源代码,可以看到从网址http://dadjokegenerator.com/api/api.php?a=j<=r&vj=0下载个别笑话
以下是从此网址下载笑话的最小示例。为简单起见,我省略了所有错误检查,并使用免费的Json.NET库进行JSON反序列化:
public class Joke
{
public int Id;
public string Setup;
public string Punchline;
public override string ToString()
{
return Setup + " " + Punchline;
}
}
public static Joke GetJoke()
{
var request = HttpWebRequest.Create("http://dadjokegenerator.com/api/api.php?a=j<=r&vj=0");
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
var jokeString = reader.ReadToEnd();
Joke[] jokes = JsonConvert.DeserializeObject<Joke[]>(jokeString);
return jokes.FirstOrDefault();
}
}
}
}
用法就是例如。
GetJoke().ToString();
答案 1 :(得分:0)