用perl获取google搜索的结果

以前google的API还开放的时候,可以直接使用CPAN上的包来进行获取,现在只能自己码代码了。。。


代码如下

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值