LoginSignup
kazu_mj
@kazu_mj (一矢 金児)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

BeautifulSoupで英語のサイトを英語のまま取得したい

解決したいこと

BeautifulSoupで解析した際にHTMLが
html lang="ja"
となるが検証ツールでは
html lang="en"
となっており検証ツールのまま取得したい

発生している問題・エラー


<!DOCTYPE html>

<html lang="ja" 

該当するソースコード

headers = {"Accept-Language": "en-US,en;q=0.5"}
res = requests.get("該当サイト",headers = headers)
res.raise_for_status()
soup = BeautifulSoup(res.text, "html.parser")
print(soup)

自分で試したこと

以下の2点を試してみました。
リクエストヘッダで"Accept-Language": "en"を指定
chromedriverの言語設定を指定

options = ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en'})
driver = webdriver.Chrome(executable_path="executable_path", desired_capabilities=options.to_capabilities())
0

1Answer

headerscookiesが足りなかったりすることが原因かもしれません.
うまくいくかはわかりませんが...

検証ツールで既にlang="en"のサイトが取得できているのなら,そのときのリクエストをcURLとしてコピペしてrequestsで扱えるように変換してみてはどうでしょう.

次のサイトのツールが便利です.

ちなみに他のChromeOptions()を扱う方法は

を見る限り

  options = webdriver.ChromeOptions()
- options.add_experimental_option('prefs', {'intl.accept_languages': 'en'})
- driver = webdriver.Chrome(executable_path="executable_path", desired_capabilities=options.to_capabilities())
+ options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
+ driver = webdriver.Chrome(executable_path="executable_path", chrome_options=options)

の方が正しいようですが,実際こちらではどうなりますでしょうか.

1

Comments

  1. @kazu_mj

    Questioner
    CURLを試してみたら出来ました。
    ありがとうございます。
    ChromeOptionの方もご指摘いただきありがとうございます。
  2. 解決されたようでよかったです.
    本質問を「クローズ」にして終了になります.

Your answer might help someone💌