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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

3179閲覧

英語サイトをスクレイピングしたいのに,日本語サイトをスクレイピングしてしまう.pythonを用いて

spectrum

総合スコア44

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2016/07/01 12:05

pythonを用いて,Jリーグの順位表をスクレイピングしようとしています.
Jリーグの順位表として,J.LEAGUE Data Site英語ページの今年の順位表をスクレイピングすることにしました.(https://data.j-league.or.jp/SFRT01/?competitionSectionIdLabel=Up+to+date&competitionIdLabel=MEIJI+YASUDA+J1+LEAGUE+1st&yearIdLabel=2016&yearId=2016&competitionId=411&competitionSectionId=0&search=search)
得られるデータに日本語が含まれると面倒だと思ったからです.
そこでpython3を用いて以下のようなコードを書き,実行しましたが,日本語ページのデータが得られてしまいました.これはなぜでしょうか?
よろしければ教えてください.お願いします.

python3

1from urllib.request import urlopen 2from bs4 import BeautifulSoup 3import codecs 4 5html = urlopen("https://data.j-league.or.jp/SFRT01/?competitionSectionIdLabel=Up+to+date&competitionIdLabel=MEIJI+YASUDA+J1+LEAGUE+1st&yearIdLabel=2016%E5%B9%B4&yearId=2016&competitionId=411&competitionSectionId=0&search=search") 6bsObj = BeautifulSoup(html) 7fDataFile = codecs.open('j1-2016.dat','w','utf-8') 8 9for child in bsObj.find("table",{"id":"search_result"}).children: 10 fDataFile.write('%s\n'%child) 11

また,得られた結果の一部も示しておきます.

<thead> <tr id="sortHeader"> <th class="wd00 bg-glay">グラフ</th> <th class="wd01 al-l bg-glay" data-sort-type="numericData">順位</th> <th class="wd02 bg-glay">チーム</th> <th class="wd03 al-l" data-sort-type="numericData">勝点</th> <th class="wd04 al-l" data-sort-type="numericData">試合</th> <th class="wd05 al-l" data-sort-type="numericData">勝</th> <th class="wd08 al-l" data-sort-type="numericData">分</th> <th class="wd09 al-l" data-sort-type="numericData">敗</th> <th class="wd12 al-l" data-sort-type="numericData">得点</th> <th class="wd13 al-l" data-sort-type="numericData">失点</th> <th class="wd14 al-l" data-sort-type="numericData" style="padding-right:10px">得失点差</th> <th class="wd17 bg-glay">直近試合の勝敗</th> <th class="wd18 bg-glay"> </th> <th class="implicit-sort-col" data-sort-append="1" data-sort-type="numericData"></th> </tr> </thead> <tbody> <tr style="background-color:"> <td class="wd00"><a class="icon-graph" href="#" onclick="javascript:window.open('/SFRT02?competitionYearId=2016&amp;competitionId=411¤tCompetitionSectionId=3727&amp;lastCompetitionSectionId=3727&amp;teamId=1','graph','width=1000,height=720,toolbar=no,menubar=no,resizable=yes,scrollbars=yes,location=no, status=no');return false;">グラフ</a></td> <td class="wd01" data-sort-value="1"> 1</td> <td class="wd02"> <a href="http://www.jleague.jp/club/kashima/profile/">鹿島アントラーズ</a> </td> <td class="wd03" data-sort-value="39">39</td> <td class="wd04" data-sort-value="17">17</td> <td class="wd05" data-sort-value="12">12</td> <td class="wd08" data-sort-value="3">3</td> <td class="wd09" data-sort-value="2">2</td> <td class="wd12" data-sort-value="29">29</td> <td class="wd13" data-sort-value="10">10</td> <td class="wd14" data-sort-value="19"> +19</td> <td class="wd17"> <img class="img-base" src="/images/common/icon_c-w2.gif"><img class="img-base-print" src="/images/common/icon_c-w.png"><img class="img-base" src="/images/common/icon_c-w2.gif"><img class="img-base-print" src="/images/common/icon_c-w.png"><img class="img-base" src="/images/common/icon_c-w2.gif"><img class="img-base-print" src="/images/common/icon_c-w.png"><img class="img-base" src="/images/common/icon_c-w2.gif"><img class="img-base-print" src="/images/common/icon_c-w.png"><img class="img-base" src="/images/common/icon_c-w2.gif"><img class="img-base-print" src="/images/common/icon_c-w.png"/></img></img></img></img></img></img></img></img></img></td> <td class="wd18"></td> <td class="implicit-sort-col" data-sort-value="80">80</td> </tr> <tr style="background-color:"> <td class="wd00"><a class="icon-graph" href="#" onclick="javascript:window.open('/SFRT02?competitionYearId=2016&amp;competitionId=411¤tCompetitionSectionId=3727&amp;lastCompetitionSectionId=3727&amp;teamId=21','graph','width=1000,height=720,toolbar=no,menubar=no,resizable=yes,scrollbars=yes,location=no, status=no');return false;">グラフ</a></td> 以下同様に他のクラブの情報がきます

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

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

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

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

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

guest

回答2

0

該当サイトを見ると、言語の切り替えはcookieによって行っています。
どういった値が入っているかは、ブラウザのデバッガで見てみればわかるかと思います。

デフォルトが日本語のようですので、英語でのcookieを再現してアクセスする必要があるかと思います。

このあたりを参考に、cookieを加味してアクセスするようにしてはいかがでしょうか?
http://pc.atsuhiro-me.net/entry/2013/12/04/232446

投稿2016/07/01 12:46

CodeLab

総合スコア1939

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

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

spectrum

2016/07/01 12:54 編集

なるほど. 恥ずかしながら,cookieを考えたことはありませんでした. 重要な進路を示して頂き有難うございます.
guest

0

ベストアンサー

Pythonは、分かりませんが、
右上のボタンのパラメータで、言語を指定している部分があったので、真似してみたら表示されました。

HTML

1https://data.j-league.or.jp/SFRT01/?competitionSectionIdLabel=Up+to+date&competitionIdLabel=MEIJI+YASUDA+J1+LEAGUE+1st&yearIdLabel=2016&yearId=2016&competitionId=411&competitionSectionId=0&search=search 23https://data.j-league.or.jp/SFRT01/?competitionSectionIdLabel=Up+to+date&competitionIdLabel=MEIJI+YASUDA+J1+LEAGUE+1st&yearIdLabel=2016&yearId=2016&competitionId=411&competitionSectionId=0&search=search&lang=en

パラメータの後ろに「&lang=en」 と追加しただけです。
やってみてください ^^

投稿2016/07/01 12:40

Mr_Roboto

総合スコア2208

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

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

spectrum

2016/07/01 12:51 編集

有難うございます.できました! 素人なのでその発想に全く至りませんでした.恐れ入ります 精進しますm(_ _)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問