利用Asp.Ne怎么对搜索引擎网址进行收录检查

发布时间:2020-12-09 17:26:27 作者:Leah
来源:亿速云 阅读:223

这期内容当中小编将会给大家带来有关利用Asp.Ne怎么对搜索引擎网址进行收录检查,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

实现原理:直接搜索你那篇文章的url地址(不带协议,但上协议也行,代码会自动去掉协议内容),如果被索引会返回搜索结果,否则会提示找不到信息。

Asp.Net检查百度,谷歌,搜狗搜索引擎是否收录文章网址源代码:

using System;
using System.Net;
using System.Text;
using System.IO;
using System.Web;
public class SearchEngineIndex
{
  public static string[] urls = { //搜索引擎检查地址
      "http://www.baidu.com/s?ie=utf-8&wd=",//百度索引url检查地址
      "https://www.google.com.hk/search?q=",//谷歌索引url检查地址
      "http://www.sogou.com/web?ie=utf8&query="//搜狗索引url检查地址
    }
    , noFindKeyword = { "抱歉,没有找到与", "找不到和您的查询", "未收录?" };//搜索引擎未索引url地址时的关键字
  /// <summary>
  /// 获取响应的编码
  /// </summary>
  /// <param name="contenttype"></param>
  /// <returns></returns>
  private static Encoding GetEncoding(string contenttype)
  {
    if (!string.IsNullOrEmpty(contenttype))
    {
      contenttype = contenttype.ToLower();
      if (contenttype.IndexOf("gb2312") != -1 || contenttype.IndexOf("gbk") != -1) return Encoding.GetEncoding(936);
      if (contenttype.IndexOf("big5") != -1) return Encoding.GetEncoding(950);
    }
    return Encoding.UTF8;
  }
  /// <summary>
  /// 使用HttpWebRequest对象,自动识别字符集
  /// </summary>
  /// <param name="url"></param>
  /// <param name="addUseragent">是否添加UserAgent,采集其他网站时防止被拦截</param>
  /// <returns></returns>
  public static string GetHtml(string url, bool addUseragent)
  {
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    if (addUseragent) request.UserAgent = "Googlebot|Feedfetcher-Google|Baiduspider";
    string html = null;
    try
    {
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      StreamReader srd = new StreamReader(response.GetResponseStream(), GetEncoding(response.ContentType));
      html = srd.ReadToEnd();
      srd.Close();
      response.Close();
    }
    catch { }
    return html;
  }
  /// <summary>
  /// 检查某个url是否被搜索引擎索引
  /// </summary>
  /// <param name="url">url地址</param>
  /// <param name="engin">0:百度 1:谷歌 2:搜狗,其他搜索引擎如bing和360直接查网址显示的结果不是直接得到网址的,有些出入,不做检查</param>
  /// <returns></returns>
  public static bool CheckIndex(string url, int engin)
  {
    if (string.IsNullOrEmpty(url)) return false;
    if (engin < 0 || engin > 2) engin = 0;
    url = urls[engin] + HttpUtility.UrlEncode(url.ToLower().Replace("http://", "").Replace("https://", ""));
    bool r = true;
    string html = GetHtml(url, true);
    if (html == null || html.IndexOf(noFindKeyword[engin]) != -1) r = false;
    return r;
  }
}



//调用方法示例

    SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 0);//检查百度索引
    SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 1);//检查谷歌索引
    SearchEngineIndex.CheckIndex("www.jb51.net/article/20101014/2902.aspx", 2);//检查搜狗索引

Asp检查百度,谷歌,搜狗搜索引擎是否收录文章网址源代码:

<%
class SearchEnginIndex
 dim urls,noFindKeyword
 private sub Class_Initialize
  '百度,谷歌,搜狗url地址索引查询地址
  urls=array("http://www.baidu.com/s&#63;ie=utf-8&wd=","https://www.google.com.hk/search&#63;q=","http://www.sogou.com/web&#63;ie=utf8&query=")
  '搜索引擎未索引url地址时的关键字
  NoFindKeyword=array("抱歉,没有找到与", "找不到和您的查询", "未收录?")
 End sub
 private function GetEncoding(contenttype)
  contenttype=lcase(contenttype)
  if instr(contenttype,"gb2312")<>0 and instr(contenttype,"gbk")<>0 then
   GetEncoding="gb2312"
  elseif instr(contenttype,"big5")<>0 then
   GetEncoding="big5"
  else
   GetEncoding="utf-8"
  end if
 end function
 private function BinToString(bin,encoding)'将2进制流数据依据编码转为对应的字符串内容
  dim obj
  set obj=Server.CreateObject("Adodb.Stream")
  obj.Type=1:obj.Mode=3:obj.Open
  obj.Write bin
  obj.Position=0:obj.Type=2:obj.Charset=encoding
  BinToString=obj.ReadText
  obj.Close:set obj=nothing
 end function
 public function GetHtml(url)
  dim xhr
  set xhr=server.CreateObject("microsoft.xmlhttp")
  xhr.open "get",url,false
  xhr.send
  encoding=GetEncoding(xhr.getResponseHeader("content-type"))
  response.CharSet=encoding
  GetHtml=BinToString(xhr.responsebody,encoding)
  set xhr=nothing
 end function
 public function CheckIndex(url,engin)
  if len(url)=0 then exit function
  if engin<0 or engin>2 then engin=1
  url=urls(engin)&server.URLEncode(url)
  dim html
  html=GetHtml(url)
  CheckIndex=instr(html,NoFindKeyword(engin))=0
 End function
end Class
set sei=new SearchEnginIndex
response.Write sei.CheckIndex("www.jb51.net/article/20101014/2902.aspx",0)'百度索引
response.Write sei.CheckIndex("www.jb51.net/article/20101014/2902.aspx",1)'谷歌索引
response.Write sei.CheckIndex("www.jb51.net/article/20101014/2902.aspx",2)'搜狗索引
set sei=nothing
 %>

上述就是小编为大家分享的利用Asp.Ne怎么对搜索引擎网址进行收录检查了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Python中怎么对ping终端进行检查
  2. python怎么对实例属性进行类型检查

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net ne

上一篇:asp.net怎么将DataSet转换为josn数据并输出

下一篇:如何在php项目中实现一个计数排序算法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》