开发者

Extract Title from web page, using just the url

开发者 https://www.devze.com 2023-03-21 23:25 出处:网络
I want to replicate some functionality from Digg.com whereby when you post a new address it automatically scans the url and finds the page title.

I want to replicate some functionality from Digg.com whereby when you post a new address it automatically scans the url and finds the page title.

I am programming in classic ASP and VBScript and using javascript. Anyone know a script to make thi开发者_开发问答s happen..?

Many thanks in advance..

Paul


This is somewhat of a rudimentary example. You should probably include some data verification.

The ASP page should be called something like getPageTitle.asp

<%
Response.Buffer = True

Dim strURL, objXMLHTTP, objXML, strContents
Dim objRegExp, strHTML, strPattern, colMatches, strTitle

strURL = Request.Form("url")

Set objXMLHTTP = Server.CreateObject ("Microsoft.XMLHTTP")
'Or if this doesn't work then try :
'Set objXMLHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

objXMLHTTP.Open "GET", strURL, False

objXMLHTTP.Send

strContents = objXMLHTTP.ResponseText

Set objXMLHTTP = Nothing

Set objRegExp = New RegExp

strPattern = "<title>(.*?)<\/title>"

objRegExp.Pattern = strPattern
objRegExp.IgnoreCase = True
objRegExp.Global = True

Set colMatches = objRegExp.Execute(strContents)

If colMatches.Count > 0 then
    strTitle = objMatches(0).Value
Else
    strTitle = ""
End If

Set objRegExp = Nothing

Response.write(strTitle)
%>

This is a basic JavaScript POST implementation. You could spruce this up a bit with any JS framework you like.

var script = "http://www.example.com/getPageTitle.asp"
var page2check = "http://www.example.com/somePageToCheck.html"

function getXMLHttpRequestObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = new getXMLHttpRequestObject();

var parameters = "url="+page2check;
http.open("POST", script, true);

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters .length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {
  if(http.readyState == 4) {
    alert(http.responseText);
  }
}
http.send(parameters);

var pageTitle = http.ResponseText

I hope this helps.


  1. Send url from clientside to serverside using javascript(ajax).
  2. Load html page by it's url using asp on serverside.
  3. Parse html page, extract title.
  4. Send title to clientside.
0

精彩评论

暂无评论...
验证码 换一张
取 消