2011-08-14 8 views
1

これは私のコードです。URLからページタイトルを取得するためにGroovyで書いたコードです。しかし、私が "Moved Permanently"と書いたウェブサイトの中には、301リダイレクトが原因であると思うものがあります。私はこれを避けるために、私が代わりに正しいページタイトル http://www.nytimes.com/2011/08/14/arts/music/jay-z-and-kanye-wests-watch-the-throne.htmlHttpUrlConnectionはコンテンツのタイトルを取得し、「Moved Permanently」を取得しました

 

     def con = (HttpURLConnection) new URL(url).openConnection() 
     con.connect() 

     def inputStream = con.inputStream 

     HtmlCleaner cleaner = new HtmlCleaner() 
     CleanerProperties props = cleaner.getProperties() 

     TagNode node = cleaner.clean(inputStream) 
     TagNode titleNode = node.findElementByName("title", true); 

     def title = titleNode.getText().toString() 
     title = StringEscapeUtils.unescapeHtml(title).trim() 
     title = title.replace("\n", ""); 
     return title 
 

答えて

1

を挿入...

私は、問題はサイトがそのそれクッキーを期待するということだと思いますリダイレクトチェーンの途中を送信し、リダイレクトチェーンが取得できない場合は、ログインページに移動します。

このコードは明らかに(そしてこれを行うには良い方法はおそらくあり)、一部はクリーンアップ必要がありますが、それは私がタイトルを抽出する方法を示しています。

@Grab('net.sourceforge.htmlcleaner:htmlcleaner:2.2') 
@Grab('commons-lang:commons-lang:2.6') 
import org.apache.commons.lang.StringEscapeUtils 
import org.htmlcleaner.* 

String location = 'http://www.nytimes.com/2011/08/14/arts/music/jay-z-and-kanye-wests-watch-the-throne.html' 
String cookie = null 
String pageContent = '' 

while(location) { 
    new URL(location).openConnection().with { con -> 
    // We'll do redirects ourselves 
    con.instanceFollowRedirects = false 

    // If we got a cookie last time round, then add it to our request 
    if(cookie) con.setRequestProperty('Cookie', cookie) 
    con.connect() 

    // Get the response code, and the location to jump to (in case of a redirect) 
    int responseCode = con.responseCode 
    location = con.getHeaderField("Location") 

    // Try and get a cookie the site will set, we will pass this next time round 
    cookie = con.getHeaderField("Set-Cookie") 

    // Read the HTML and close the inputstream 
    pageContent = con.inputStream.withReader { it.text } 
    } 
} 

// Then, clean paceContent and get the title 
HtmlCleaner cleaner = new HtmlCleaner() 
CleanerProperties props = cleaner.getProperties() 

TagNode node = cleaner.clean(pageContent) 
TagNode titleNode = node.findElementByName("title", true); 

def title = titleNode.text.toString() 
title = StringEscapeUtils.unescapeHtml(title).trim() 
title = title.replace("\n", "") 

println title 

はそれが役に立てば幸い!

0

の「恒久的に移動し、」しまった、このウェブサイトをHttpURLConnectionのは、右のURLに従ってみましょう。例えば、正しいページタイトル

を取得するにはどうすればよいですHttpUrlConnectionでsetInstanceFollowRedirects(true)を呼び出す必要があります。つまり、最初の行の後、私はリダイレクト自分自身を管理する場合、私はこの作業を取得することができます con.setInstanceFollowRedirects(真)

+0

私はそれを試みましたが、それでも動作しませんでした。私はsetInstainceFollowRedirects(true)はデフォルトだと思います。しかし、お返事いただきありがとうございます。 – toy

+0

ええ、私は投稿する前に自分自身を試していたはずです。私はあなたの症状を再現しましたが、なぜその理由が分かりません。私はHttpUrlConnectionの代わりにHttpBuilderを試してみました。それはadditonal設定なしのリダイレクトに従っています。しかし、結果のコンテンツをHtmlCleanerに渡すことはできませんでした。 – mmigdol

+0

物事に影響を与えているのはどうでもいいのですか? –

関連する問題