如何在我的页面上自动显示第一张谷歌图片
How do I automatically display the first google image on my page?
>我有一个页面,它会自动从MySQL数据库中检索两个随机条目,并要求用户在每次刷新页面时进行比较。如何将这两个字符串自动转换为它们的第一个谷歌图片结果?这是我到目前为止所拥有的:
<?php //I retrieve the names and group names here//
$firstpersonaName = (string) $row["personaName"];
$firstpersonaGroupName = (string) $row["groupName"];
$firstpersonaGroupNameForGoogle = preg_split('( )', $firstpersonaGroupName);
?> //then convert any group names containing spaces into arrays here//
<?php //then here I build the query that displays a google image page//
$newname = '';
foreach ($firstpersonaGroupNameForGoogle as $firstpersonaPartofGroupName) {
$newname = $firstpersonaPartofGroupName . '+';
}
$newname = rtrim($newname, "+");
echo "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch';
?>
这给了我这样的东西:https://www.google.com/search?q=charlie+always+sunny&tbm=isch
那么我如何获取该链接并将其转换为第一张图像的链接呢?或者真的是前一对中的任何一对。(在本例中:http://cdn3.denofgeek.us/sites/denofgeekus/files/sunny_0.jpg)
好的,这就是我最终为每个查询随机生成两张图片的方法:
首先,我下载了这个并将其添加到与网页相同的目录中:http://simplehtmldom.sourceforge.net/
然后我只是在我希望图片显示的div 中添加了这个 PHP 代码:
<?php
// Include the php dom parser
include_once 'simple_html_dom.php';
//build the google images query
$newname = '';
foreach ($firstpersonaGroupNameForGoogle as $firstpersonaPartofGroupName) {
$newname = $firstpersonaPartofGroupName . '+';
}
$newname = rtrim($newname, "+");
//echo "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch';
$newname = "https://www.google.com/search?q=" . $firstpersonaName . "+" . $newname . '&tbm=isch';
//use parser on queried page
$html = file_get_html($newname);
//echo $html;
//create an array for all pics on page
$picarray = array();
$picurl = '';
// Find all images
foreach($html->find('img') as $element) {
//echo $element->src . '<br>';
$picurl = $element->src;
array_push($picarray,$picurl);
}
//then pick two random ones
$picurl = $picarray[array_rand($picarray)];
echo "<img src=" . $picurl . ">";
$picurl = $picarray[array_rand($picarray)];
echo "<img src=" . $picurl . ">";
?>
它们的分辨率很小(大约 150 像素),但这实际上非常适合我正在尝试做的事情。如果你想检索一张非缩略图图片,那就是一个完全不同的蠕虫罐头。
相关文章:
- 谷歌图表在运行应用程序时只能显示一次
- 有没有一种方法可以使用Javascript检索谷歌表单ITSELF(而不是电子表格)的数据
- 在最初的查询/表格绘制中只提取谷歌电子表格的一列
- 在谷歌应用程序脚本中,如何在满足条件的情况下只发送一次电子邮件
- 有没有一种方法可以在没有谷歌云控制台的情况下使用带有JavaScript的谷歌硬盘
- 如何点击谷歌饼图进入下一步行动
- 一次显示一个标记-谷歌地图API v3
- 有没有一种方法可以避开特定的道路或在谷歌方向中进行协调
- 如何在我的页面上自动显示第一张谷歌图片
- Javascript一直说谷歌是未定义的
- 有没有一种谷歌支持的方法来截取gap.client JS库
- 谷歌电子表格脚本为包含某个单词的行着色,并自动将该行移动到另一张工作表
- 我怎样才能把这张谷歌地图弄黑呢?
- 在一张地图上显示多个谷歌地图标记和方向
- 如何查看手机是否至少使用过一次谷歌地图
- 我怎样才能将这张谷歌地图集中在用户的本地位置
- 我试图在一个给定地址的网站上加载多个谷歌地图.最后一张地图是唯一显示的地图
- 在谷歌地图上放一张图片
- 谷歌电子表格脚本.仅适用于一张图纸
- 多种出行模式,使用谷歌地图 API 渲染一张地图