在 Python 上与分析用户一起使用 GoogleSearch?

Use GoogleSearch on Python with a profiling user?

我正在尝试使用 Google 在 Python 中搜索来抓取网络,但我希望它能考虑到我过去的搜索。有没有办法登录,然后使用 Python 在 Google 中搜索?

这是我使用的部分代码:

from googlesearch import search

for j in search(query, stop=n, lang=lang):
    #request al browser
    req = urllib.request.Request(j, headers={'User-Agent': "Magic Browser"})
    cj = CookieJar()
    try:
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    except:
        pass

试试这个代码(请注意:google搜索不支持代理,但我知道如果你需要如何添加这个功能):

from googlesearch import search
query = "apple iphone news 2019"
for i in search(\
    query,        # The query you want to run
    tld  = 'com.ua', # The top level domain
    lang = 'en',  # The language
    num  = 10,    # Number of results per page
    start= 0,     # First result to retrieve
    stop = None,  # Last result to retrieve
    pause= 2.0,   # Lapse between HTTP requests
    ):
    my_results_list.append(i)
    print(i)

请密切注意 'tld' 参数 - 它们必须等于顶级 google 域(域级别从右到左上升)。例如,对于我的领土 Google 服务器 DNS eq 'www.google.com.ua' 和 'tld'='com.ua'。 请参阅 this list 以获得每个地区 Google 服务器的完整列表

更新: 对于支持代理:

proxy_support = urllib.request.ProxyHandler({'http' : 'http://IP-Address:Port', 
                                             'https': 'http://IP-Address:port'})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)