如何获得谷歌搜索结果页面而不是搜索查询的结果?

时间:2019-05-21 01:05:55

标签: python url search beautifulsoup

我正在为我的一个项目使用python制作语音助手,并且大多数命令都是从google搜索结果页面的信息框中获取信息。 (例如,天气会在所有搜索结果之前的小框中显示天气和更多有用的信息)。

我的问题是:我可以获取google搜索结果页面而不是实际搜索结果吗?

我已经尝试过使用请求库,并且考虑过使用网络浏览器库,但是问题是网络浏览器库打开了一个实际的标签,并且没有给我html页面,而且我不希望这样新标签页将打开,我只需要html页面。

from bs4 import BeautifulSoup
import requests


def start_search(searchq):  # searchq is the search query (for example weather)
    r = requests.get("https://www.google.com/search?q=" + searchq)  # supposed to be the search results page 
    soup = BeautifulSoup(r.text, 'html.parser')
    results = soup.find_all("div", attrs={"class": "vk_gy vk_h"})  # the info I want to extract

我的预期结果是搜索结果页面,我可以在其中搜索所需的信息,但我只是获得了搜索查询找到的第一个结果。

0 个答案:

没有答案