【问题标题】:Why this is printing 0? [closed]为什么这是打印0? [关闭]
【发布时间】:2021-07-06 23:30:22
【问题描述】:

我正在尝试在 PHP 中进行 Instagram 关注者计数。 (因为 JavaScript 不工作)

我得到了这个 PHP 脚本,但它不工作。

<?php
$url = 'https://www.instagram.com/cristiano/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
     $count = $match[1];
} echo $count;
?>

【问题讨论】:

  • 我认为是因为您的 preg_match 语句返回 false ,因此认为它永远不会更新。
  • 你应该检查$str的内容,因为php.net/manual/en/…可能被禁用了
  • 另外,您正在下载的 HTML 中可能不存在该字符串。相反,它存在于由 JavaScript 生成的 DOM 中。
  • 我认为$str是一个HTML,但我对PHP不太了解。
  • 离题:无法重现 / 需要调试细节 3v4l.org/Jtu8K 如果这不是关于preg_match() 调用,而是关于抓取url,然后删除问题中file_get_contents() 行之后的所有内容。 stackoverflow.com/q/35234096/2943403 有什么对你有帮助的吗?

标签: php html


【解决方案1】:

供您参考 ->>

1. Instagram 已将 #followed_by 更改为 #edge_followed_by(来源:view-source:https://www.instagram.com/cristiano),这是它返回 0 的原因之一。

2. 将“?__a=1”添加到 Insta 个人资料网址(https://www.instagram.com/cristiano/?__a=1),您将获得 Json,只需解码 Json 并获得价值。

3. 这些不是从 Instagram 获取信息的合法方式。我更喜欢使用Instagram API,它易于使用且免费。

更新

4. 或者您可以从 &lt;meta&gt; 标签而不是 &lt;script&gt; 标签获取关注者(示例 - &lt;meta content="310m Followers, 476 Following, 3,095 Posts - See Instagram photos and videos from Cristiano Ronaldo (@cristiano)" name="description" /&gt;

【讨论】:

  • 谢谢!我在做 Instagram API。
  • 是的,没错。
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 2010-11-06
  • 2023-01-14
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多