无法获取英文搜索结果

SIM*_*SIM 2 python beautifulsoup web-scraping python-3.x

我用 python 编写了一个脚本来执行谷歌搜索并获取结果。目前工作正常。然而,我面临的唯一问题是,我得到的大部分结果都是我的母语,而不是英语,而当我在谷歌浏览器中执行相同的搜索时,我得到的是英语结果。

我如何修改我的脚本以获得所有英文搜索结果?

这是我到目前为止的尝试:

from bs4 import BeautifulSoup
import requests

link = "http://www.google.com/search?q={}"

def fetch_results(query):
    res = requests.get(link.format(query.replace(" ","+")))
    soup = BeautifulSoup(res.text,"lxml")
    for item in soup.select("span"):
        print(item.get_text())

if __name__ == '__main__':
    fetch_results('india')
Run Code Online (Sandbox Code Playgroud)

mat*_*tch 6

您可以使用 urlhttps://www.google.com/search?q={}&hl={}并设置hlen英语来指定语言过滤器。

google 搜索 api 文档中描述了各种选项(此链接适用于 xml,但许多选项也可用作查询参数):https ://developers.google.com/custom-search/docs/xml_results#wsQueryTerms