用perl获取google搜索的结果
2012-07-13 18:54
411 查看
以前google的API还开放的时候,可以直接使用CPAN上的包来进行获取,现在只能自己码代码了。。。
代码如下
LWP::UserAgent 用来下载网页
URI::Escape 用来对中文进行转义,生成google链接中的奇奇怪怪的编码
use Encode 对我们输入的检索关键词编码至utf8
代码如下
sub google_search{ my $keyword = $_[0]; print "keyword is $keyword\n"; #we must first convert our search term into utf8, then can we use uri_escape to encode the term to search-engine like encoding my $search = encode("utf-8",decode("gb2312", $keyword)); $search = uri_escape($search); print "keyword is $search\n"; my $ua = new LWP::UserAgent; #Construct the request object $ua->agent( "Mozilla/5.0 (X11; Linux i686; rv:2.0.0) Gecko/20100101" ); #Google checks UAs, so set this to something common $ua->timeout( 10 ); #Give up after 10 seconds of waiting $ua->max_redirect( 0 ); #We only want the URL that google sends, so stop after we submit $ua->env_proxy; #Load proxy info from the environment, if any is set my $response = $ua->get( "http://www.google.com/search?q=".$search #The URL to request ) or die( $! ); #my $firstUrl = $response->contents; #This contains the link of the result page return encode("utf8", $response->decoded_content()); #convert to utf8 }这里用到了3个package:
LWP::UserAgent 用来下载网页
URI::Escape 用来对中文进行转义,生成google链接中的奇奇怪怪的编码
use Encode 对我们输入的检索关键词编码至utf8
相关文章推荐
- jsonp跨域请求实现获取Google搜索结果
- curl 获取google搜索结果
- 编写Python脚本来获取Google搜索结果的示例
- 编写Python脚本来获取Google搜索结果的示例
- 编写Python脚本来获取Google搜索结果的示例
- 应用程序获取Google搜索结果(二)
- 用Nightmarejs 获取百度搜索结果数量
- google搜索结果金三角现象及其意义
- Google搜索结果排列算法—Google工程师详述
- 影响Google搜索结果的因素
- Hsiaoyang:Google搜索结果页面分析
- chrome浏览器使用google搜索,搜到结果却打不开网页
- Google中文版推出搜索结果翻译功能BETA版
- Google 会帮你在目标网站用你的关键字搜索,直接给你搜索结果页面?
- 抓取 google 搜索结果
- Google算法更新!搜索结果中将出现一个域名的大量页面
- Chrome_01_点击 Google搜索结果在新的页面打开
- [013] 百度地图API之城市POI搜索-获取所有结果(Android)
- 百度地图API之城市POI搜索-获取所有结果(Android)
- Google悄悄推行“链接 + 数据" 富内容摘要的搜索结果