当前位置:编程学习 > C#/ASP.NET >>

求救,抓取百度URL的关键字

 if (System.Web.HttpContext.Current.Request.UrlReferrer != null)
                   pUrl = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();

实际用户搜索URL:
http://www.baidu.com/s?ie=gb2312&bs=%B9%E2%D7%D3%C4%DB%B7%F4%D2%C7%20%BC%DB%B8%F1&sr=&z=&cl=3&f=8&wd=%D7%F8%CA%BD%CF%E3%D1%AC%D2%A9%D4%A1%B2%D5%20%BC%DB%B8%F1&ct=0

但是确得到如下URL:
http://www.baidu.com/s?ie=gb2312&bs=%B9%E2%D7%D3%C4?+%BC?&sr=&z=&cl=3&f=8&wd=%D7%F8?%CF%E3???+%BC?&ct=0

请问怎么解决啊? --------------------编程问答-------------------- 例如用户在百度搜索  “坐式香熏药浴舱 价格 ”进入我的网站,
我用
if (System.Web.HttpContext.Current.Request.UrlReferrer != null) 
                  pUrl = System.Web.HttpContext.Current.Request.UrlReferrer.ToString(); 

抓取上一页面URL,
但是得到却不正确,
请教大家如何才能得到正确的URL啊?






--------------------编程问答-------------------- 编码有问题 --------------------编程问答-------------------- 这么解决啊?

我的目的是获取用户用百度搜索的关键字:“坐式香熏药浴舱 价格 ”,
help!!! --------------------编程问答-------------------- Server.UrlDecode(str_url);

Server.UrlEncode(str_url);  

的具体应用,哈。 --------------------编程问答-------------------- // pUrl = HttpUtility.UrlDecode(pUrl, System.Text.Encoding.GetEncoding("gb2312"));
   pUrl = Server.UrlDecode(pUrl);

都用遍了,没有能解决啊

--------------------编程问答-------------------- http://www.softfault.com --------------------编程问答-------------------- 你如果获取过来就有问题的话,那就不知道问题在那里了 --------------------编程问答-------------------- 获取过来没问题,是编码有问题 --------------------编程问答--------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Text;

namespace BxgAdmin
{
    public class keyword
    {
        private string[][] _Enginers = new string[][] { 
        new string[]{"google","utf8","q"},
        new string[]{"baidu","gb2312","wd"},
        new string[]{"yahoo","utf8","p"},
        new string[]{"yisou","utf8","search"},
        new string[]{"live","utf8","q"},
        new string[]{"tom","gb2312","word"},
        new string[]{"163","gb2312","q"},
        new string[]{"iask","gb2312","k"},
        new string[]{"soso","gb2312","w"},
        new string[]{"sogou","gb2312","query"},
        new string[]{"zhongsou","gb2312","w"},
        new string[]{"3721","gb2312","p"},
        new string[]{"openfind","utf8","q"},
        new string[]{"alltheweb","utf8","q"},
        new string[]{"lycos","utf8","query"},
        new string[]{"onseek","utf8","q"}
    };
       
        //搜索引擎名称
        private string _EngineName = "";
        public string EngineName
        {
            get
            {
                return _EngineName;
            }
        }
        private string keyurl;
        //搜索引擎编码
        private string _Coding = "utf8";
        public string Coding
        {
            get
            {
                return _Coding;
            }
        }
        //搜索引擎关键字查询参数名称
        private string _RegexWord = "";
        public string RegexWord
        {
            get
            {
                return _RegexWord;
            }
        }
        private string _Regex = @"(";
        public void EngineRegEx(string myString)
        {
            for (int i = 0, j = _Enginers.Length; i < j; i++)
            {
                if (myString.Contains(_Enginers[i][0]))
                {
                    _EngineName = _Enginers[i][0];
                    _Coding = _Enginers[i][1];
                    _RegexWord = _Enginers[i][2];
                    _Regex += _EngineName + @"\.+.*[?/&]" + _RegexWord + @"[=:])(?<key>[^&]*)";
                    break;
                }
            }
        }
        public string SearchName(string str)
        {
            for (int i = 0, j = _Enginers.Length; i < j; i++)
            {
                if (str.Contains(_Enginers[i][0]))
                {
                    keyurl = _Enginers[i][0];
                    break;
                }
            }
            return keyurl;
        }
        public string SearchKey(string myString)//获得关键字
        {
            EngineRegEx(myString.ToLower());
            if (_EngineName != "")
            {
                Regex myReg = new Regex(_Regex, RegexOptions.IgnoreCase);

                Match matche = myReg.Match(myString);
                myString = matche.Groups["key"].Value;
                //去处表示为空格的+
                myString = myString.Replace("+", " ");

                if (_Coding == "gb2312")
                {
                    myString = GetUTF8String(myString);
                }
                else
                {
                    myString = Uri.UnescapeDataString(myString);
                }
            }
            return myString;
        }
        public string GetUTF8String(string myString)
        {
            Regex myReg = new Regex("(?<key>%..%..)", RegexOptions.IgnoreCase);

            MatchCollection matches = myReg.Matches(myString);
            string myWord;

            for (int i = 0, j = matches.Count; i < j; i++)
            {
                myWord = matches[i].Groups["key"].Value.ToString();
                myString = myString.Replace(myWord, GB2312ToUTF8(myWord));
            }
            return myString;
        }
        public string GB2312ToUTF8(string myString)
        {
            string[] myWord = myString.Split('%');
            byte[] myByte = new byte[] { Convert.ToByte(myWord[1], 16), Convert.ToByte(myWord[2], 16) };

            Encoding GB = Encoding.GetEncoding("GB2312");
            Encoding U8 = Encoding.UTF8;
            myByte = Encoding.Convert(GB, U8, myByte);

            char[] Chars = new char[U8.GetCharCount(myByte, 0, myByte.Length)];
            U8.GetChars(myByte, 0, myByte.Length, Chars, 0);

            return new string(Chars);
        }
    }
}
--------------------编程问答-------------------- www.bj2scmm.com --------------------编程问答-------------------- 当初我抓的是有道的搜索,把你的所要传的值转的百度的搜索页面,然后把那个页面把不用的链接都剃掉,在写到div中,只是给你点思路,后台因为有道的那个页面样式换了,所以整过搜索功能都用不了!呵呵
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,