猿问

控制 Google 搜索中的 Javascript 翻译

该代码使用下面的init .py执行谷歌搜索


def search(term, num_results=10,, 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 翻译的结果并只获得一种语言的搜索结果?


温温酱
浏览 149回答 1
1回答

桃花长相依

我很抱歉。请忽略这个问题。我犯了一个小错误,将三重引用的注释放在代码另一部分的函数中。它禁用了有效传递的参数。从下面删除三重引用评论后,我只得到英文结果:for google_url in search(query,&nbsp; # The query you want to run&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;,&nbsp; # User interface language (host language)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_results = 10,&nbsp; # Number of results per page&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lr="lang_en"&nbsp; # Langauge of the documents received'''lr - parameter is implemented in __init__.py of googlesearch&nbsp; &nbsp; 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.&nbsp; &nbsp; (ex. cr=countryCA)gl - boosts search results whose country of origin matches the parameter value.&nbsp; &nbsp; (ex. gl=uk)'''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ):
随时随地看视频慕课网APP

相关分类

Python
我要回答