如何在没有 API 的情况下获取 Instagram 页面的关注者数量

How to get number of followers of an instagram page without API

我正在尝试以编程方式获取页面的关注者数量,确切数量(例如 521356)或(521K)都可以。

我试过下载数据来下载整个页面,但我似乎找不到关注者的数量

System.Net.WebClient wc = new System.Net.WebClient();
            byte[] raw = wc.DownloadData("https://www.instagram.com/gallery.delband/");

            string webData = System.Text.Encoding.UTF8.GetString(raw);
            textBox1.Text = webData;

我希望能够获得关注者的数量,但我无法使用网络浏览器方法找到数据。

WebClient 只是发出一个简单的 HTTP 请求,return 如今对于很多网站而言,这将变得非常少。您基本上会得到一个告诉浏览器 "Great, now get that javascript bundle over there to get started" 的页面。因此,要获得您想要的信息,您需要更高级的东西,例如 CefSharp to actually load the page and execute scripts and everything. Preferably you'd use CefSharp.OffScreen 以不显示浏览器 window。然后就可以解析出你想要的信息了

问题是,如果不执行 JavaScript,您将无法像在浏览器中看到的那样获取 instagram 网页。而System.Net.WebClient不执行js。

但是,如果您分析页面的 html 来源,您会发现关注者计数包含在带有 name="description":

<meta> 标签中
<meta content="88.5k Followers, 1,412 Following, 785 Posts - See Instagram photos and videos from گالری نقره عیار ۹۲۵‌‌ ترکیه (@gallery.delband)" name="description" />

要从源头获取此信息,请使用正则表达式:

var pattern = @"<meta content=\""([0-9k KMm\.,]+) Followers, .*\"" name=\""description\"" \/>";
var match = Regex.Match(webData, pattern);
var followers = match.Groups[1];

该模式的意思是:查找一个以 <meta content=" 开头的字符串,后跟一个由字符 0-9、k、K、M、m、','、'.' 组成的动态字符串。或 ' '(实际关注者计数)后跟文本“关注者”,然后是任何文本,但以 name="description" /> 结尾。因为我们将动态部分括起来,正则表达式系统将这个动态值作为组结果提供给我们。