2017-01-28 5 views
3

Googleから"define"+wordクエリをリクエストして音韻綴りを収集しようとしています。ご存知のように、 "define"の後に単語の辞書定義を取得します。SeleniumでFirefoxを起動するとシステムデフォルトのFirefox設定と異なる

しかし、FirefoxをSeleniumから起動すると、googleがドイツ語に設定されるようになります。これは私が望むものではありません。

enter image description here

問題は、今私は、同じキーワードを誤った結果を取得することです:私は、Firefoxを起動すると、手動では、私が欲しいものある、英語に設定されている

enter image description here

私が使用してスクリプトがこれまでに次のようになります。

import time 
from selenium import webdriver 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys 


capabilities = DesiredCapabilities.FIREFOX 
capabilities["marionette"] = True 
browser = webdriver.Firefox(capabilities=capabilities) 

try: 
    browser.get('https://google.com') 
    search = browser.find_element_by_name('q') 
    search.send_keys("define car") 
    search.send_keys(Keys.RETURN) 
    time.sleep(5) 


except Exception as e: 
    print (format(e)) 

スクリプトから起動されたFirefoxを手動で起動したように動作させる方法を教えてください。

答えて

2

あなただけintl.accept_languagesプリファレンスを設定することにより、en-USを受け入れるようにFirefoxのを強制することができ、あなたはあまりにも国コードを変更することができます。

profile = webdriver.FirefoxProfile() 
profile.set_preference('intl.accept_languages', 'en-US, en') 
profile.set_preference('browser.search.countryCode', 'US') 

capabilities = DesiredCapabilities.FIREFOX 
capabilities["marionette"] = True 

browser = webdriver.Firefox(firefox_profile=profile, capabilities=capabilities) 

は私のために働きました。また、intl.accept_languagesde-DE, deに設定しようとしましたが、Google検索ページがドイツ語で表示されました。

https://google.comではなく、http://www.google.com/ncr(「ncr」は「国のリダイレクトなし」を意味します)に移動できます。

+0

tryブロックの上のコードブロックをコードに置き換えましたが、googleはまだドイツ語に設定されています。私のシステムの言語は英語でもbtwです。それはIPから来ています。 –

+0

@lotolmencre興味深い、それは英語とドイツ語の両方のために私のために働く - 私はこのように言語を制御することができます.. – alecxe

+0

しかし、アングロホンの国からのIPがありますか? –

関連する問題