如何阅读外部页面的标题?(How to read an external page's title?)

编程入门 行业动态 更新时间:2024-10-27 06:33:24
如何阅读外部页面的标题?(How to read an external page's title?)

我认为使用jQuery是可能的,但任何ASP.NET服务器端代码也适用于我的情况。

使用jQuery,我可以加载一个页面,例如div,并过滤div为<title>标签,但我认为对于重页面,首先阅读所有内容然后读取标题标签是不好的..或者也许它有一个非常简单的解决方案? 无论如何,我无法从互联网上找到任何相关信息。 谢谢

I think it's possible with jQuery, but any ASP.NET serverside code is good for my situation too.

With jQuery I can load a page to for example a div, and filter the div for <title> tag, but I think for heavy pages, it is not good to first read all of the content and then read the title tag.. or maybe it has a very simple solution? anyways I couldnt find anything about that from internet. thanks

最满意答案

好的,感谢cjjer和Boo,我刚刚阅读了有关正则表达式的更多内容,最后下面的代码对我有用。

Dim qq As New System.Net.WebClient Dim theuri As New Uri(TextBox1.Text) Dim res As String = qq.DownloadString(theuri) Dim re As Regex = New Regex("<title\b[^>]*>(.*?)</title>", RegexOptions.Singleline) Dim ma As Match = re.Match(res) If Not ma Is Nothing And ma.Success Then Response.Write(ma.Groups(1).Value.ToString()) Else Response.Write("error") End If

但无论如何,问题仍然存在,这段代码正在下载整个页面并通过它寻找,其中一个重型网站花了超过2或3秒才完成,但似乎这是我所知道的唯一方法: 有什么建议可以改进这段代码吗?

okay thanks to cjjer and Boo, I've just read more about regex and finally the code below is working for me.

Dim qq As New System.Net.WebClient Dim theuri As New Uri(TextBox1.Text) Dim res As String = qq.DownloadString(theuri) Dim re As Regex = New Regex("<title\b[^>]*>(.*?)</title>", RegexOptions.Singleline) Dim ma As Match = re.Match(res) If Not ma Is Nothing And ma.Success Then Response.Write(ma.Groups(1).Value.ToString()) Else Response.Write("error") End If

but anyways, the problem remains, this code is downloading the whole page and seeking through it, which one heavy websites it took more than 2 or 3 secconds to complete, but seems it is the only way as far as I know :| Is there any suggestions to refine this code?

更多推荐

本文发布于:2023-08-07 13:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   标题   read   title   page

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!