在 Google 搜索中控制 Javascript 翻译

control Javascript translation in Google search

代码使用下面的 init.py

执行 google 搜索
def search(term, num_results=10, lang="en", lr="lang_en"):
    usr_agent = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/61.0.3163.100 Safari/537.36'}

    def fetch_results(search_term, number_results, language_code):
        escaped_search_term = search_term.replace(' ', '+')

        google_url = 'https://www.google.com/search?q={}&num={}&hl={}&lr={}'.format(escaped_search_term, number_results+1,
                                                                              language_code, lr)
...

一些返回的链接使用 javascript 翻译网站:

<script type="text/javascript">
        var home = '/de/', root = '/', country = 'ch', language = 'de', w = {
            "download_image": "Bild download (Niedrige Qualität)",
...

另一个例子:

<script>
                dataLayer.push({
                    'brand' : 'Renault',
                    'countryCode' : 'BE',
                    'googleAccount' : 'UA-23041452-1',
                    'adobeAccount' : 'renaultbeprod',
                    'languageCode' : 'nl',
...

有没有办法过滤掉通过 javascript 翻译的结果并只获得一种语言的搜索结果?

抱歉。请忽略这个问题。我犯了一个小错误,将三重引用的注释放在代码另一部分的函数中。它使参数无法有效传递。

从下面删除三重引用评论后,我得到的结果只有英文:

for google_url in search(query,  # The query you want to run
                lang='en',  # User interface language (host language)
                num_results = 10,  # Number of results per page
                lr="lang_en"  # Langauge of the documents received
'''
lr - parameter is implemented in __init__.py of googlesearch
    It should be handled only here.
Other useful search parameters not used yet are:
cr - restricts search results to documents originating in a particular country.
    (ex. cr=countryCA)
gl - boosts search results whose country of origin matches the parameter value.
    (ex. gl=uk)
'''
                ):