Asp.Net、asp實(shí)現(xiàn)的搜索引擎網(wǎng)址收錄檢查程序
使用asp.net或者asp檢查某個(gè)url地址,某篇文章是否被搜索引擎,如百度,谷歌,搜狗收錄。
實(shí)現(xiàn)原理:直接搜索你那篇文章的url地址(不帶協(xié)議,但上協(xié)議也行,代碼會(huì)自動(dòng)去掉協(xié)議內(nèi)容),如果被索引會(huì)返回搜索結(jié)果,否則會(huì)提示找不到信息。
Asp.Net檢查百度,谷歌,搜狗搜索引擎是否收錄文章網(wǎng)址源代碼:
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="http://搜狗索引url檢查地址 } , noFindKeyword = { "抱歉,沒(méi)有找到與", "找不到和您的查詢", "未收錄?" };//搜索引擎未索引url地址時(shí)的關(guān)鍵字 /// <summary> /// 獲取響應(yīng)的編碼 /// </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對(duì)象,自動(dòng)識(shí)別字符集 /// </summary> /// <param name="url"></param> /// <param name="addUseragent">是否添加UserAgent,采集其他網(wǎng)站時(shí)防止被攔截</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> /// 檢查某個(gè)url是否被搜索引擎索引 /// </summary> /// <param name="url">url地址</param> /// <param name="engin">0:百度 1:谷歌 2:搜狗,其他搜索引擎如bing和360直接查網(wǎng)址顯示的結(jié)果不是直接得到網(wǎng)址的,有些出入,不做檢查</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; } } //調(diào)用方法示例 SearchEngineIndex.CheckIndex("www.dbjr.com.cn/article/20101014/2902.aspx", 0);//檢查百度索引 SearchEngineIndex.CheckIndex("www.dbjr.com.cn/article/20101014/2902.aspx", 1);//檢查谷歌索引 SearchEngineIndex.CheckIndex("www.dbjr.com.cn/article/20101014/2902.aspx", 2);//檢查搜狗索引
Asp檢查百度,谷歌,搜狗搜索引擎是否收錄文章網(wǎng)址源代碼:
<% 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地址時(shí)的關(guān)鍵字 NoFindKeyword=array("抱歉,沒(méi)有找到與", "找不到和您的查詢", "未收錄?") 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進(jìn)制流數(shù)據(jù)依據(jù)編碼轉(zhuǎn)為對(duì)應(yīng)的字符串內(nèi)容 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.dbjr.com.cn/article/20101014/2902.aspx",0)'百度索引 response.Write sei.CheckIndex("www.dbjr.com.cn/article/20101014/2902.aspx",1)'谷歌索引 response.Write sei.CheckIndex("www.dbjr.com.cn/article/20101014/2902.aspx",2)'搜狗索引 set sei=nothing %>
相關(guān)文章
asp.net 不用GridView自帶刪除功能,刪除一行數(shù)據(jù)
數(shù)據(jù)表一定要有個(gè)ID的主鍵值,你的gridview要設(shè)定一下DataKeyNames="ID"這個(gè)屬性值,接下的事件就好多了,寫(xiě)個(gè)OnRowDeleting事件就可以了。2009-11-11ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象,本文講解設(shè)計(jì)及實(shí)現(xiàn)方法,相關(guān)代碼托管到GITHUB,需要的朋友可以參考下2015-03-03干貨來(lái)襲! C# 7.0 新特性(VS2017可用)
干貨來(lái)襲! 為大家分享了C# 7.0 新特性,VS2017可用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03asp.net文件上傳功能(單文件,多文件,自定義生成縮略圖,水印)
上傳功能,是大家經(jīng)常用到了,可能每一個(gè)項(xiàng)目都可以會(huì)用到。網(wǎng)上到處都有上傳功能的代碼。比我寫(xiě)的好的有很多。我這里也僅是分享我的代碼。2011-09-09asp.net部署到IIS常見(jiàn)問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了asp.net部署到IIS常見(jiàn)問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12輕量級(jí)ORM框架Dapper應(yīng)用之Dapper支持存儲(chǔ)過(guò)程
這篇文章介紹了Dapper支持使用存儲(chǔ)過(guò)程的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03web用戶控件調(diào)用.aspx頁(yè)面里的方法
今天在一QQ技術(shù)群有朋友問(wèn): 他在web用戶控件中(.ascx)中放了一個(gè)dropdownlist控件,一個(gè)textbox控件和一個(gè)button控件。2009-04-04