如何从Google Play动态获取应用程序图标

7

我正在开发一个应用程序,它会显示一系列应用程序,我想从Google Play商店获取这些应用程序的图标以在列表中显示它们。如果有任何方法可以做到这一点,请告诉我。


尝试使用android-market-api - yorkw
3个回答

3

最近我在更新我的作品集网站时,不得不自己解决这个问题,所以我有一些代码可以提供给您:)我使用的是php,但我不确定您想要使用什么语言。首先,我使用Chrome浏览器的“查看” -> “开发者” -> “开发者工具”检查了包含应用程序的页面的源代码。然后,我使用DOM遍历查找可以用于标识应用图标的内容。我发现这样一个元素: screenshot 这表明应用程序图标被包含在一个类名为“doc-banner-icon”的div中——我在其他地方找不到这个类,所以我认为它是唯一的。然后,在我的php代码中,我使用simpledomparser加载URL、定位图标并输出其URL,如下所示:

<?php
include('simple_html_dom.php');

$html = file_get_html("https://play.google.com/store/apps/details?id=com.smithyproductions.swissarmycarrot"); //put your app id here

$bannerImage = $html->find('.doc-banner-icon'); //the class we found before

$img = $bannerImage[0]->find('img'); //find the img tag inside the div

$imgUrl = $img[0]->src; //get its src url

$arr = array(); //in my own example I filled this array with other things like the title an screenshots

$arr['imgUrl'] = $imgUrl;

echo json_encode($arr); //output it in an easy to read format

?>

这会导致类似以下的结果
{'imgUrl','https://lh6.ggpht.com/1WMU4E3lnbjz5yxLHxsPrJAJPw3uYZ8LXk3QkD1EKOxwOcHu0W9QyGlpM5AfrKYEVzzi=w124'}

需要记住的一点是,谷歌可能随时更改所有内容的呈现和布局,因此请准备好在发生更改时更新您的应用程序 :)


它会给出错误信息:致命错误:在/home/quicksai/public_html/google/simple_html_dom.php中调用未定义的函数mb_detect_encoding()。 - user2552725
看起来现在他们把结构改成了'div.cover-container img.cover-image'。虽然是个好主意,但完全不可靠。 - camelCaseCoder

1
谷歌不断改变页面结构,目前我找不到任何官方资源来处理这个问题,类似于苹果商店的方式。无论如何,以下是我截至今天(2019年6月)使用的PHP代码。顺便说一句,在我的代码中,一旦我成功获取图标URL,我会将其缓存在我的数据库中,以便下次不必再在Google Play商店中查找。
          try{
            $lookupData = @file_get_contents('https://play.google.com/store/apps/details?id=com.google.android.gm&hl=en');
            // Not valid any more 
            $pregString = '/<meta itemprop="image" content="(.*?)"\/>/';

            //June 2019
            $pregString = '/<img src="(.*?)" srcset=".*" class=".*" aria-hidden="true" alt="Cover art" itemprop="image">/';
            preg_match($pregString, $lookupData, $output);

        } catch (\Throwable $e) {
            $error = $e->getMessage();
            if (strpos($error, '404 Not Found') === false) {
                //unknown error
            }else{
                //Package not found, use default icon or something
            }
        }
        if(isset($output[1])){
            //Store $output[1];
        }else{
            //icon not found, use default icon or something
        }

1

我修改了roarster的代码,使其能够与Play Store的新网页配合使用,并进行了简化:

<?php
include('simple_html_dom.php');

$play_link = $_GET['playlink']; //Play store link

$html = file_get_html($play_link); //put your app id here

$bannerImage = $html->find('div.cover-container'); //the class we found before

$img = $bannerImage[0]->find('img'); //find the img tag inside the div

$imgUrl = $img[0]->src; //get its src url

$arr = array(); //in my own example I filled this array with other things like the title an screenshots

$arr['imgUrl'] = $imgUrl;

echo json_encode($arr); //output it in an easy to read format

?>

现在你加载例如:yourwebpage.com/script.php?playlink=https://play.google.com/store/apps/details?id=com.igg.android.im,然后你会得到结果 ;)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接