Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从国家自然科学基金里面爬取所有的基金项目大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
use LWP::UserAgent;
use HTML::TreeBuilder;
use LWP::Simple;
use URI;
use Encode;
use LWP::UserAgent;
use http::Cookies;
###########################################################
###1,首先构造一系列网页地址
###########################################################
@list_url=();
@download_url=();
foreach (1..2927)#首先自己看看需要爬多少个页面         {
         my $url = URI->new('http://npd.nsfc.gov.cn/projectSearch!search.action?');
         my($field,$pagE) = ("C",$_);#对C这个领域做查询,页面累积变化
         $url->query_form
         (
           # All form pairs:
           'project.applyCode'  => $field,#搜索的领域,此处固定为C,是生命科学的代码
           'currentPage' => $page,);
         push @list_url,$url;
         }
#自己构造所有的网页地址,以便后面的浏览器访问
#map{print "$_\n"}   @list_url;   #测试了一些,网址生成没有问题
###########################################################
###2,然后构造perl的模拟浏览器
###########################################################
my $tmp_ua = LWP::UserAgent->new;    #UserAgent用来发送网页访问请求
$tmp_ua->timeout(15);                ##连接超时时间设为15秒
$tmp_ua->protocols_allowed( [ 'http','https' ] ); ##只允许httphttps协议
$tmp_ua->agent(
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;.NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"
  ) ; ##用来在header中告诉服务器你用的是什么"浏览器",设置文件头的User-Agent
  my $cookie_jar = '.mozilla/firefox/bg146ia6.default/cookies.sqlite';
  $tmp_ua->cookie_jar(http::Cookies->new('file'=>"$ENV{'HOME'}/$cookie_jar",'autosave'=>1));
###########################################################
###3,最后一个个爬取我们构造好的网站地址,找到相应的信息
###########################################################
open FH,">aa.txt";  #可以保存我们需要的东西
$url_index="http://npd.nsfc.gov.cn/";
 #必须要先访问一下主页并且保存cookies才能爬查询网页
my $response = $tmp_ua->get($url_indeX);  #访问了主页的同时也储存了cookies
#$html=$response->content;
#print encode("cp936",decode("utf8",$html));
#my $response = $tmp_ua->get($list_url[3]);
#$html=$response->content;
#print encode("cp936",$html));   #经测试,可以访问每个项目页面了
#这些都只是一步一步的测试
#接下来做正事,访问所有的页面,一个个爬取信息
foreach (@list_url)#对我们自己合成的目标url做循环爬取适合的链接
          {
          my $response = $tmp_ua->get($_);
          $html=$response->content;
           my $tree = HTML::TreeBuilder->new; # empty tree
           $tree->parse($html) or print "error : parse html ";
           @dl_list=$tree->find_by_attribute("class","time_dl") or print "error : cAnnot find time_dl";
                  

foreach (@dl_list)
       {
       $dt_node=$_->look_down(_tag=>'dt');
       $dt_a_node=$dt_node->look_down(_tag=>'a');#dt标签里面的a是基金项目的网站介绍
       print FH $url_index.$dt_a_node->attr('href');
       print FH "\t";
       print FH encode("cp936",$dt_a_node->as_text()));#基因项目的名字
       print FH "\t";
       @node=$_->look_down(_tag=>'dd'); #dd表格里面的是关于这个基金的各种描述
       map{print FH encode("cp936",$_->as_text()))}@node;
       print FH "\n";
      }
}

大佬总结

以上是大佬教程为你收集整理的从国家自然科学基金里面爬取所有的基金项目全部内容,希望文章能够帮你解决从国家自然科学基金里面爬取所有的基金项目所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: