使用 python 从谷歌搜索中获取响应

问题描述 投票:0回答:0

我正在尝试从谷歌搜索中获取文本。我的想法是,我在普通的谷歌搜索中使用 python 进行搜索,然后打印出搜索结果旁边正确站点上的文本。但是我找到的代码不起作用。


from googlesearch 
import search
from bs4 import BeautifulSoup
import requests

def google_search(query):
    results = search(query, num_results=1)
    for result in results:
        response = requests.get(result)
        soup = BeautifulSoup(response.content, 'html.parser')
        answer = soup.find('div', class_='kno-rdesc')
        if answer:
            return answer.text
response = google_search("Was ist die Hauptstadt von Deutschland")
print(response)

所以基本上它应该返回正确的框,有时您可以在正确的站点上找到它。我希望有人能帮忙。谢谢

python google-chrome search
© www.soinside.com 2019 - 2024. All rights reserved.