这里是网站上的代码html。
<span class="def">
to make something
<a class="query" title="clear" href="http://dictionary.cambridge.org/dictionary/british/clear"></a>
or
<a class="query" title="easy" href="http://dictionary.cambridge.org/dictionary/british/easy"></a>
to
<a class="query" title="understand" href="http://dictionary.cambridge.org/dictionary/british/understand"></a>
by
<a class="query" title="describing" href="http://dictionary.cambridge.org/dictionary/british/describe"></a>
or giving
<a class="query" title="information" href="http://dictionary.cambridge.org/dictionary/british/information"></a>
about it:
怎么做?我希望得到这样的结果:制作某种东西或通过或给予它
我有httpAgilityPack的代码。
string url = "http://dictionary.cambridge.org/dictionary/british/appeal";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
WebRequest request = WebRequest.Create(url);
WebResponse response = await request.GetResponseAsync();
StreamReader stream = new StreamReader(response.GetResponseStream());
string content = await stream.ReadToEndAsync();
List<string> list = new List<string>();
foreach (...)
{
...
}
我希望每个人都能提供帮助。
答案 0 :(得分:1)
private async void print_def(String uristring)
{
using (HttpClient client = new HttpClient())
{
//verify uri is a valid
Uri outuri;
if(Uri.TryCreate(uristring,UriKind.RelativeOrAbsolute,out outuri))
{
//try to get the site
HttpResponseMessage response=new HttpResponseMessage();
try
{
response = await client.GetAsync(outuri);
}
catch (HttpRequestException exc)
{
System.Diagnostics.Debug.WriteLine("Error geting Html from site");
}
//check if site returned success code 200
if (response.IsSuccessStatusCode)
{
String html = await response.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine("succes");
HtmlDocument htmldoc = new HtmlDocument();
htmldoc.LoadHtml(html);
foreach (HtmlNode defnode in htmldoc.DocumentNode.Descendants("span").Where(spannode => spannode.Attributes.Contains("class")&&spannode.Attributes["class"].Value.Equals("def")))
{
System.Diagnostics.Debug.WriteLine(defnode.InnerText.ToString());
//put here whaterver you want to do with the text
}
}
}
}
}
你可以将这个功能复制粘贴到你的项目中,并传递你想要作为字符串抓取的网址。对我有用,希望能回答你的问题。