質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Perl

Perlは多目的に使用される実用性が高い動的プログラミング言語のひとつです。

Q&A

解決済

2回答

2641閲覧

スクレイピング本のサンプルですが404 Not Foundというエラーが出る

YoSa

総合スコア49

Perl

Perlは多目的に使用される実用性が高い動的プログラミング言語のひとつです。

0グッド

1クリップ

投稿2016/05/23 05:29

編集2016/05/23 06:50

以下のコードを実行すると
404 Not Found at script.pl line 31
と出てしまいます。
少し前の本でURLが変わったのか分かりませんが
どう対応していいのか分かりません・・・

use strict; use warnings; use SOAP::Lite; use URI; use LWP::Simple; use XML::Simple; use encoding 'UTF-8'; # 使用しているプラットフォームにあわせてください。 binmode(STDERR, ':raw :encoding(UTF-8)'); # 同上 use Encode; # Google APIを使用するために必要となる情報 my $google_key = "yourgoogleapikey"; my $google_wdsl = "GoogleSearch.wsdl"; my $gsrch = SOAP::Lite->service("file:$google_wdsl"); my $query = join(" ", @ARGV) . ' site:co.jp'; my $numOfSample = 50; # Google から取得する結果の数を指定する。 my $start = 0; my %siteName; my %siteURL; while ($start < $numOfSample) { my $num = $numOfSample - $start; $num = 10 if $num > 10; # 検索を実行する。 my $result = $gsrch->doGoogleSearch($google_key, $query, $start, $num, "false", "", "false", "lang_jp", "", ""); # 結果を格納する。 foreach my $hit (@{$result->{'resultElements'}}){ my @args = ( cli => 10, dat => 'snba', ver => '7.0', url => $hit->{URL}, ); my $base = 'http://data.alexa.com/data'; my $uri = URI->new( $base ); $uri->query_form( @args ); my $content = decode('utf8', get($uri->as_string)); #print $content; exit; my $xml = XMLin( $content ); if (defined($xml->{SD}{POPULARITY}{TEXT})) { # トラフィックランクのあるもののみを登録する。 my $popularity = int($xml->{SD}{POPULARITY}{TEXT}); $siteName{$popularity} = $xml->{DMOZ}{SITE}{TITLE} # DMOZがあるもののみ登録する。 if defined($xml->{DMOZ}{SITE}{TITLE}); $siteURL{$popularity} = $xml->{DMOZ}{SITE}{BASE} if defined($xml->{DMOZ}{SITE}{BASE}); } } $start += 10; } # 結果をランクの高いもの順に表示する。 foreach my $p (sort { $a <=> $b } keys %siteName) { print "$p\t$siteName{$p}\t$siteURL{$p}\n"; } コード

以下はGoogleSearch.wsdlの中身です

<definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="urn:GoogleSearch" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" targetNamespace="urn:GoogleSearch" name="GoogleSearch" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types> <s:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:GoogleSearch"> <s:complexType name="GoogleSearchResult"> <s:all> <s:element name="documentFiltering" type="s:boolean" /> <s:element name="searchComments" type="s:string" /> <s:element name="estimatedTotalResultsCount" type="s:int" /> <s:element name="estimateIsExact" type="s:boolean" /> <s:element name="resultElements" type="s0:ResultElementArray" /> <s:element name="searchQuery" type="s:string" /> <s:element name="startIndex" type="s:int" /> <s:element name="endIndex" type="s:int" /> <s:element name="searchTips" type="s:string" /> <s:element name="directoryCategories" type="s0:DirectoryCategoryArray" /> <s:element name="searchTime" type="s:double" /> </s:all> </s:complexType> <s:complexType name="ResultElement"> <s:all> <s:element name="summary" type="s:string" /> <s:element name="URL" type="s:string" /> <s:element name="snippet" type="s:string" /> <s:element name="title" type="s:string" /> <s:element name="cachedSize" type="s:string" /> <s:element name="relatedInformationPresent" type="s:boolean" /> <s:element name="hostName" type="s:string" /> <s:element name="directoryCategory" type="s0:DirectoryCategory" /> <s:element name="directoryTitle" type="s:string" /> </s:all> </s:complexType> <s:complexType name="ResultElementArray"> <s:complexContent mixed="false"> <s:restriction base="soapenc:Array"> <s:attribute d7p1:arrayType="s0:ResultElement[]" ref="soapenc:arrayType" xmlns:d7p1="http://schemas.xmlsoap.org/wsdl/" /> </s:restriction> </s:complexContent> </s:complexType> <s:complexType name="DirectoryCategoryArray"> <s:complexContent mixed="false"> <s:restriction base="soapenc:Array"> <s:attribute d7p1:arrayType="s0:DirectoryCategory[]" ref="soapenc:arrayType" xmlns:d7p1="http://schemas.xmlsoap.org/wsdl/" /> </s:restriction> </s:complexContent> </s:complexType> <s:complexType name="DirectoryCategory"> <s:all> <s:element name="fullViewableName" type="s:string" /> <s:element name="specialEncoding" type="s:string" /> </s:all> </s:complexType> </s:schema> </types> <message name="doGetCachedPage"> <part name="key" type="s:string" /> <part name="url" type="s:string" /> </message> <message name="doGetCachedPageResponse"> <part name="return" type="s:base64Binary" /> </message> <message name="doSpellingSuggestion"> <part name="key" type="s:string" /> <part name="phrase" type="s:string" /> </message> <message name="doSpellingSuggestionResponse"> <part name="return" type="s:string" /> </message> <message name="doGoogleSearch"> <part name="key" type="s:string" /> <part name="q" type="s:string" /> <part name="start" type="s:int" /> <part name="maxResults" type="s:int" /> <part name="filter" type="s:boolean" /> <part name="restrict" type="s:string" /> <part name="safeSearch" type="s:boolean" /> <part name="lr" type="s:string" /> <part name="ie" type="s:string" /> <part name="oe" type="s:string" /> </message> <message name="doGoogleSearchResponse"> <part name="return" type="s0:GoogleSearchResult" /> </message> <portType name="GoogleSearchPort"> <operation name="doGetCachedPage"> <input message="s0:doGetCachedPage" /> <output message="s0:doGetCachedPageResponse" /> </operation> <operation name="doSpellingSuggestion"> <input message="s0:doSpellingSuggestion" /> <output message="s0:doSpellingSuggestionResponse" /> </operation> <operation name="doGoogleSearch"> <input message="s0:doGoogleSearch" /> <output message="s0:doGoogleSearchResponse" /> </operation> </portType> <binding name="GoogleSearchBinding" type="s0:GoogleSearchPort"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> <operation name="doGetCachedPage"> <soap:operation soapAction="urn:GoogleSearchAction" /> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> <operation name="doSpellingSuggestion"> <soap:operation soapAction="urn:GoogleSearchAction" /> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> <operation name="doGoogleSearch"> <soap:operation soapAction="urn:GoogleSearchAction" /> <input> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </input> <output> <soap:body use="encoded" namespace="urn:GoogleSearch" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> </output> </operation> </binding> <service name="GoogleSearchService"> <port name="GoogleSearchPort" binding="s0:GoogleSearchBinding"> <soap:address location="http://api.google.com/search/beta2" /> </port> </service> </definitions> コード

曖昧な質問ですが動くようにしたいです
アドバイスお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

サンプルコードの中で

Perl

1my $base = 'http://data.alexa.com/data';

と定義されているURLにアクセスすると「404 Not Found」となりますから致し方ないですね…

ちょっと調べてみたところ、このURLは、元々有料で公開されているAPIを無料で利用可能なURLで、ドキュメントも存在しない事から無保証で一時的に公開されていた物のようです。

無料でAlexaのトラフィックデータを取得できるAPI(ドキュメント無し) | Lancork

試していませんが、有料版は現在でも有効で利用可能である可能性もあります。しかし、APIの仕様が変更になった(あるいは無料版とは最初から異なる)というケースも考えられるため、保証はありません。

スクレイピングの場合、対象サイトに合わせて常にメンテナンスが必要なので、印刷されたサンプルでは有りがちな問題です。ですから、このサンプルについては、処理法方のエッセンスを学に留め、諦めて他のサンプルコードの動作確認に着手した方が得策だと思います。

投稿2016/05/23 07:38

pi-chan

総合スコア5936

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

どうやら、Google SOAP Search APIは、2009年ごろに既にサービス終了してしまっているみたいですね。
すみません、前回の質問のときに調べていなかったので気づきませんでした。

"The SOAP Search API is no longer available. Thank you for your interest.
Google Developers
https://developers.google.com/soap-search/

A well earned retirement for the SOAP Search API - The official Google Code blog
http://googlecode.blogspot.jp/2009/08/well-earned-retirement-for-soap-search.html

参考にされている本は、かなり古い本なんでしょうか?
コンピューターの特にインターネット関連の情報は移り変わりが早いので、なるべく新しい書籍を利用された方が良いと思います。

投稿2016/05/23 07:17

argius

総合スコア9388

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YoSa

2016/05/23 07:30

この本はspideringhacksという本で調べたら2005年発行していたようです。 やはりこの業界は最新の本じゃないとこういう目にあうんですね。 勉強になりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問