在 c# 中获取网页的计数 +1

本文关键字:网页 获取 | 更新日期: 2023-09-27 18:35:14

我想要一种方法来获取网页网址并返回其+1的计数。我搜索了谷歌,只是找到这个,方法是:

int GetPlusOnes(string url) 
{
     string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
     string postData = @"[{""method"":""pos.plusones.get"",""id"":""p"",""params"":{""nolog"":true,""id"":""" + url + @""",""source"":""widget"",""userId"":""@viewer"",""groupId"":""@self""},""jsonrpc"":""2.0"",""key"":""p"",""apiVersion"":""v1""}]";
     System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(googleApiUrl);
     request.Method = "POST";
     request.ContentType = "application/json-rpc";
     request.ContentLength = postData.Length;
     System.IO.Stream writeStream = request.GetRequestStream();
     UTF8Encoding encoding = new UTF8Encoding();
     byte[] bytes = encoding.GetBytes(postData);
     writeStream.Write(bytes, 0, bytes.Length);
     writeStream.Close();
     System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
     System.IO.Stream responseStream = response.GetResponseStream();
     System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8);
     string jsonString = readStream.ReadToEnd();
     readStream.Close();
     responseStream.Close();
     response.Close();
    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
    int count = Int32.Parse(json[0]["result"]["metadata"]["globalCounts"]["count"].ToString().Replace(".0", ""));
    return count;
}

但是有一个错误

var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);

因为Deserialize方法有两个参数,我不知道第二个参数。有谁知道第二个参数是什么或知道另一种工作正常的方法?

更新我也试过

    public static Regex REGEX_GETURLCOUNT =
    new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>('d*)</div>");
    public int GetPlusOnes(string url)
    {
        string fetchUrl =
            "https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
            HttpUtility.UrlEncode(url) + "&count=true";
        WebClient wc=new WebClient();
        string response = wc.DownloadString(fetchUrl);
        Match match = REGEX_GETURLCOUNT.Match(response);
        if (match.Success)
        {
            return int.Parse(match.Groups[1].Value);
        }
        return 0;
    }

match.Success总是false

在 c# 中获取网页的计数 +1

最近不得不做这样的事情,我使用了:

 public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int test = GetPlusOnes("http://www.allfancydress.com/"); //Returns 11 plus ones
            Response.Write("Plus ones: " + test.ToString());
        }
        public static Regex REGEX_GETURLCOUNT =
         new Regex(@"<div[^>]+id=""aggregateCount""[^>]+>('d*)</div>");
        public static int GetPlusOnes(string url)
        {
            string fetchUrl =
                "https://plusone.google.com/u/0/_/+1/fastbutton?url=" +
                HttpUtility.UrlEncode(url) + "&count=true";
            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create(fetchUrl);
            string response = new StreamReader(request.GetResponse()
                .GetResponseStream()).ReadToEnd();
            Match match = REGEX_GETURLCOUNT.Match(response);
            if (match.Success)
            {
                return int.Parse(match.Groups[1].Value);
            }
            return 0;
        }
    }

希望这有帮助

*编辑了我的自定义代理类,您只需剪切并粘贴它,它就会为您工作。