生信常用论坛seq-answer里面所有帖子爬取 生信常用论坛bio-star里面所有帖子爬取 这个是爬虫专题第一集,主要讲如何分析bio-star这个网站并爬去所有的帖子列表,及标签列表等等,前提是读者必须掌握perl,然后学习perl的LWP模块,可以考虑打印那本书读读,挺有用的! http:/// 这个是首页 http:///forums/forumdisplay.php?f=18 这个共570个页面需要爬取 http:///forums/forumdisplay.php?f=18&order=desc&page=1 http:///forums/forumdisplay.php?f=18&order=desc&page=570 <tbody id=”threadbits_forum_18″>这个里面包围这很多<tr>对, 前五个<tr>对可以跳过,里面的内容不需要 这样就可以捕获到所有的目录啦! 首先我们看看如何爬去该论坛主页的板块构成,然后才进去各个板块里面继续爬去帖子。 接下来看进入各个板块里面爬帖子的代码,可以直接复制张贴使用的! [perl] use LWP::Simple; use HTML::TreeBuilder; use Encode; use LWP::UserAgent; use HTTP::Cookies; my $tmp_ua = LWP::UserAgent->new; #UserAgent用来发送网页访问请求 $tmp_ua->timeout(15); ##连接超时时间设为15秒 $tmp_ua->protocols_allowed( [ 'http’, 'https’ ] ); ##只允许http和https协议 $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)" ) ; $base=’https://www.'; open FH_IN,"index.txt"; while (<FH_IN>) { chomp; @F=split; open FH_OUT,">index-$F[1].txt"; $total_pages=int($F[2]/40)+1; foreach (1..$total_pages){ my $url = URI->new("$F[0]/?"); my($sort,$page) = ("update",$_);# $url->query_form( 'page’ => $page, 'sort’ => $sort, ); &get_each_index($url,’FH_OUT’); print $url."\n"; } } sub get_each_index{ my ($url,$handle)=@_; $response = $tmp_ua->get($url); $html=$response->content; my $tree = HTML::TreeBuilder->new; # empty tree $tree->parse($html) or print "error : parse html "; my @list_title=$tree->find_by_attribute('class’,"post-title"); foreach (@list_title) { my $title = $_->as_text(); my $ref = $_->find_by_tag_name('a’)->attr('href’); print $handle "$base$href,$title\n"; } } [/perl] 这样就可以爬去帖子列表了 https://www./t/rna-seq rna 1573 https://www./t/R R 1309 https://www./t/snp snp 1268 等等“““““““““““““““““““““““““““““` 帖子文件如下,在我的群里面共享了所有的代码及帖子内容,欢迎加群201161227,生信菜鸟团! 生信常用论坛seq-answer里面所有帖子爬取 这个是爬虫专题第二集,主要讲如何分析seq-answer这个网站并爬去所有的帖子列表,及标签列表等等,前提是读者必须掌握perl,然后学习perl的LWP模块,可以考虑打印那本书读读,挺有用的! 其实爬虫是个人兴趣啦,跟这个网站没多少关系,本来一个个下载,傻瓜式的重复也能达到目的。我只是觉得这样很有技术范,哈哈,如何大家不想做傻瓜式的操作可以自己学习学习,如果不懂也可以问问我! http:///这个是主页 http:///forums/forumdisplay.php?f=18 这个共570个页面需要爬取 其中f=18 代表我们要爬去的bioinformatics板块里面的内容 http:///forums/forumdisplay.php?f=18&order=desc&page=1 http:///forums/forumdisplay.php?f=18&order=desc&page=570 <tbody id=”threadbits_forum_18″>这个里面包围这很多<tr>对, 前五个<tr>对可以跳过,里面的内容不需要 这样就可以捕获到所有的目录啦! 我这个直接把所有代码贴出了啦 [perl] use LWP::Simple; use HTML::TreeBuilder; use Encode; use LWP::UserAgent; use HTTP::Cookies; my $tmp_ua = LWP::UserAgent->new; #UserAgent用来发送网页访问请求 $tmp_ua->timeout(15); ##连接超时时间设为15秒 $tmp_ua->protocols_allowed( [ 'http’, 'https’ ] ); ##只允许http和https协议 $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)" ) ; open FH_OUT ,">bioinformatics.csv"; $total_pages=571; foreach (1..$total_pages){ my $url = URI->new("http:///forums/forumdisplay.php?"); my($f,$page) = (18,$_);# $url->query_form( 'f’ => $f, 'order’=> 'desc’, 'page’ => $page, ); &get_each_index($url,’FH_OUT’); print $url."\n"; } sub get_each_index{ my ($url,$handle)=@_; $response = $tmp_ua->get($url); $html=$response->content; my $tree = HTML::TreeBuilder->new; # empty tree $tree->parse($html) or print "error : parse html "; $tmp=$tree->find_by_attribute("id","threadbits_forum_18"); next unless $tmp; my @list_tr=$tmp->find_by_tag_name('tr’); shift @list_tr;shift @list_tr;shift @list_tr;shift @list_tr;shift @list_tr; foreach (@list_tr) { my @list_td=$_->find_by_tag_name('td’); #print $_->as_text; next unless @list_td>4; my $brief=$list_td[2]->attr('title’); my $title=$list_td[2]->find_by_tag_name('a’)->as_text(); my $href=$list_td[2]->find_by_tag_name('a’)->attr('href’); my $author=$list_td[3]->as_text(); #print $handle "$base$href\t$title\t$author\t$brief\n"; print $handle "$base$href\t$title\t$author\n"; } } [/perl] 帖子列表如下: 共17109个帖子。 |
|