Asp.Net、asp实现的搜索引擎网址收录检查程序

所属分类: 网络编程 / ASP.NET 阅读数: 945
收藏 0 赞 0 分享

使用asp.net或者asp检查某个url地址,某篇文章是否被搜索引擎,如百度,谷歌,搜狗收录。

实现原理:直接搜索你那篇文章的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?ie=utf-8&wd=","https://www.google.com.hk/search?q=","http://www.sogou.com/web?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
 %>

更多精彩内容其他人还在看

MVC5下拉框绑定的方法(单选)

这篇文章主要为大家详细介绍了MVC5下拉框绑定,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

.NET Core 2.0 Preview2 发布汇总

这篇文章主要为大家详细介绍了.NET Core 2.0 Preview2 发布汇总的相关内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

MVVM模式下WPF动态绑定展示图片

这篇文章主要为大家详细介绍了MVVM模式下WPF动态绑定展示图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Visual Studio Debugger七个鲜为人知的小功能

这篇文章主要为大家详细介绍了Visual Studio Debugger七个鲜为人知的小功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

asp.net core中灵活的配置方式详解

这篇文章主要给的阿加介绍了关于在asp.net core中灵活的配置方式的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
收藏 0 赞 0 分享

详解使用asp.net mvc部分视图渲染html

为了提升用户体验,一般我们采用ajax加载数据然后根据数据渲染html,渲染html可以使用前端渲染和服务器端渲染,有兴趣的可以了解一下
收藏 0 赞 0 分享

WPF实现定时刷新UI界面功能

这篇文章主要为大家详细介绍了WPF实现定时刷新UI界面功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

.net接入支付宝的支付接口

这篇文章主要为大家详细介绍了.net接入支付宝的支付接口,H5网站接入支付宝的支付接口,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Visual Studio 2017 针对移动开发的新特性汇总

Visual Studio是世界上最好的IDE之一,下面就让我们一起来看看Visual Studio 2017中有哪些功能使得移动开发变得更加容易,感兴趣的朋友通过本文学习下吧
收藏 0 赞 0 分享

ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据的传递

本文主要介绍了ASP.Net WebAPI与Ajax进行跨域数据交互时Cookies数据传递的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多