将图片网址从Google搜索保存到MySQL [英] Save image url from Google search to MySQL

查看:103
本文介绍了将图片网址从Google搜索保存到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在Google图片搜索中输入单词时,将返回一个页面.此页面包含许多带有缩略图视图的图片.我想将这些图像的位置URL保存在我的数据库(MySQL)中.我需要用PHP编写此代码,并且要保存前10张图像的URL. 我正在设计一个动态页面,我将从数据库中选择这些图像URL地址,并将它们显示在我的动态页面上.

When we enter a word in Google image search, a page is returned. This page contains many pictures with thumbnail view. I want to save the location URL of these images in my database (MySQL). I need to code this in PHP and I want to save URL of first 10 images. I am designing a dynamic page, I will pick these image URL addresses from database and will show these on my dynamic page.

我已经尝试了很多,问题是我的完整URL没有保存,因为它包含许多MySQL无效字符.我已经搜索了许多站点并找到了不同的功能,但是对此我仍然不清楚:我在构思如何完成这项工作方面失败了. 有人可以给我一些PHP代码或过程吗?

I have already tried a lot and problem is my complete URL is not saved because it contains many invalid characters for MySQL. I have searched many sites and found different functions but I am still not clear about this: I am at failure in devising an idea how I can do this job. Can somebody give me some PHP code or a procedure how it could be done?

推荐答案

我建议您使用PHP的

I would suggest you should use PHP's DOM Library. It is very powerful and allows parsing any DOM structure. Refer some of its examples and you could implement it easily.

这个想法是,您研究Google返回的页面的HTML结构,并相应地使用DOM库来解析特定标签.据我所知,图像被组织为<ul><li>标签,例如:

The idea is that you study the HTML structure of the page returned by Google and accordingly use the DOM library for parsing specific tags. From what I see, the images are organized as <ul> and <li> tags, like:

<ul class="rg_ul" data-pg="1" data-cnt="6">
    <li class="rg_li" data-row="1" style="width:216px;height:162px"></li>
    <li class="rg_li" style="width:231px;height:162px"></li>
    <li class="rg_li" style="width:218px;height:162px"></li>
    <li class="rg_li" style="width:216px;height:162px"></li>
    <li class="rg_li" style="width:216px;height:162px"></li>
    <li class="rg_li" style="width:217px;height:162px"></li>
</ul>

在每个<li>标签中,还有其他标签,其中一个是<a>.该标签似乎具有2个属性-"imgrefurl"和"imgurl"-可能会为您提供所需的图像.您需要探索这2个属性中的哪个属性.

Within each <li> tag there are additional tags, one of which is <a>. This tag seems to be having 2 attributes - "imgrefurl" and "imgurl" - that might give you the image you need. Which of these 2 attributes you need is for you to explore.

或者,在<li>中有一个<img>标记,该标记具有包含实际图像二进制文件的"src"属性.因此,您也可以解析它.请注意,该二进制文件是针对您在搜索页上看到的图片的,而不是实际的图片.

Alternatively, there is an <img> tag within <li> that has a "src" attribute containing the actual image binary. So you may parse it as well. Please note that the binary is for the image that you see on the search page and not the actual image.

对于某些指向DOM的指针,此方法可能有用- http://www.php.net/manual/en/domelement.getelementsbytagname.php http://www.php.net/manual/zh/domelement.hasattribute.php -读取所有<li>标签,然后使用"rg_li"类对其进行解析.

For some pointers to DOM, this method might be useful - http://www.php.net/manual/en/domelement.getelementsbytagname.php and http://www.php.net/manual/en/domelement.hasattribute.php - to read all <li> tags and then parse for the ones using class "rg_li".

我希望以上说明有意义

这篇关于将图片网址从Google搜索保存到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆