Php 以编程方式进行Google搜索并处理结果

Php 以编程方式进行Google搜索并处理结果,php,node.js,google-search,google-image-search,Php,Node.js,Google Search,Google Image Search,我想使用PHP或Node.js进行谷歌搜索。我还没有决定使用哪一个;这取决于这个问题的答案是什么,哪些更容易实现(我想做的其他事情在两种语言中都很容易) 在这里得到建议之后,我想处理结果,获得链接,结果的数量(只有当结果的数量很大时),等等 搜索的是图像URL 有什么建议吗?谷歌已经实施了很多保护措施,以确保其搜索引擎不会被刮伤。然而,谷歌必须继续工作,这就是关键所在。所以到目前为止,我发现最好的谷歌抓取方法就是控制一个真正的网络浏览器 如果你想走那条路,就有硒。然而,我更喜欢我的程序是独立的,

我想使用PHP或Node.js进行谷歌搜索。我还没有决定使用哪一个;这取决于这个问题的答案是什么,哪些更容易实现(我想做的其他事情在两种语言中都很容易)

在这里得到建议之后,我想处理结果,获得链接,结果的数量(只有当结果的数量很大时),等等

搜索的是图像URL


有什么建议吗?

谷歌已经实施了很多保护措施,以确保其搜索引擎不会被刮伤。然而,谷歌必须继续工作,这就是关键所在。所以到目前为止,我发现最好的谷歌抓取方法就是控制一个真正的网络浏览器

如果你想走那条路,就有硒。然而,我更喜欢我的程序是独立的,而不是依赖于安装的web浏览器(我在无头服务器上运行大多数程序)。所以我更喜欢使用基于webkit的完整浏览器(如Safari和Konqueror)驱动的javascript

Phantomjs脚本往往比较冗长,因此大多数人将其与casperjs、node horseman或DreammareJS等包装器一起使用(还有很多,搜索npm)

下面是google从节点horseman网页上抓取的一个示例:

var Horseman = require('node-horseman');
var horseman = new Horseman();

var numLinks = horseman
  .open('http://www.google.com')
  .type('input[name="q"]', 'github')
  .click("button:contains('Google Search')")
  .waitForNextPage()
  .count("li.g");

console.log("Number of links: " + numLinks);

horseman.close();
如果您知道如何使用开发人员工具检查页面,您将知道如何使用phantomjs编写刮板



一句警告的话。不要频繁下载谷歌搜索,否则谷歌可能会将你的脚本检测为机器人,并暂时禁止你。确保在搜索之间等待适当的时间。

您需要使用代理以避免被禁止。私有代理工作得最好,拥有的越多,刮取的速度就越快,延迟10-50次或线程数较低。如果你能负担100+的费用,那么你真的可以飞了

这就是你可以获得链接和结果数量的方式:

const axios=require(“axios”);
const searchString=“cats”;
const encodedString=encodeURI(searchString);
常量AXIOS_选项={
标题:{
“用户代理”:
“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43”,
},
参数:{q:encodedString,tbm:“isch”},
};
const decodeUrl=url=>decodeURIComponent(JSON.parse(`${url}`))
函数getLinks(){
返回轴
.得到(`https://www.google.com/search`,AXIOS_选项)
.then(函数({data:html}){
常量模式=
/\[1、\[0、(?[\d\w\-\+)、\[“https:\/\/(?:[^”]+)、\d+,\d+\]\s、\[(?:\/\/(?:[^”]+)、\d+,\d+\]/gm;
const matches=html.matchAll(模式);
return[…matches].map({groups:{url}})=>decodeUrl(url))
});
}
getLinks()。然后((链接)=>
console.log(links,“\n结果数:”,links.length)
);
输出:

[
  'https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png',
  'https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/article_thumbnails/other/cat_relaxing_on_patio_other/1800x1200_cat_relaxing_on_patio_other.jpg',
  'https://cdn.vox-cdn.com/thumbor/FnkGvXbx1mKIzIx8hL9V2r-L144\\u003d/0x0:2560x1536/1200x800/filters:focal(1076x564:1484x972)/cdn.vox-cdn.com/uploads/chorus_image/image/69113629/fake_cats.0.jpg',
...
Number of results:  100
"https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png"
"https://c.files.bbci.co.uk/12A9B/production/_111434467_gettyimages-1143489763.jpg"
...
Number of results:  100
或者,您可以使用来自SerpApi的。它解决验证码问题,旋转代理,并利用完整的浏览器集群

用法示例:

const SerpApi=require('google-search-results-nodejs');
const search=new SerpApi.GoogleSearch(“secret_api_key”);
常量参数={
问:“猫”,
tbm:“isch”,
ijn:“0”
};
常量回调=函数(数据){
data.image\u results.map(el=>{
控制台日志(el.原件);
});
log(“结果数:”,data.image\u results.length);
};
//将结果显示为JSON
json(参数,回调);
输出:

[
  'https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png',
  'https://img.webmd.com/dtmcms/live/webmd/consumer_assets/site_images/article_thumbnails/other/cat_relaxing_on_patio_other/1800x1200_cat_relaxing_on_patio_other.jpg',
  'https://cdn.vox-cdn.com/thumbor/FnkGvXbx1mKIzIx8hL9V2r-L144\\u003d/0x0:2560x1536/1200x800/filters:focal(1076x564:1484x972)/cdn.vox-cdn.com/uploads/chorus_image/image/69113629/fake_cats.0.jpg',
...
Number of results:  100
"https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png"
"https://c.files.bbci.co.uk/12A9B/production/_111434467_gettyimages-1143489763.jpg"
...
Number of results:  100
免责声明,我为SerpApi工作


刮谷歌搜索是一个坏主意idea@Dragon...好主意是什么?你会怎么解决?我不会你不能用程序搜索谷歌为什么这是个坏主意?@Dagon:谁说你不能用程序搜索谷歌?总是有幻影我在测试horseman时发现这个问题,有什么想法吗??