【问题标题】:How to grab text data from Google search info bar如何从 Google 搜索信息栏中获取文本数据
【发布时间】:2019-08-06 00:17:05
【问题描述】:

我需要从谷歌搜索引擎信息栏中获取文本数据。如果有人在谷歌搜索引擎上使用关键字“siemens”进行搜索。一个小的信息栏出现在谷歌搜索结果的右侧。我想为该信息栏收集一些文本信息。我如何使用请求和 Beautifulsoup 来做到这一点。这里有一些关于我写的代码。

from bs4 import BeautifulSoup as BS
import requests
from googlesearch import search
from googleapiclient.discovery import build

url = 'https://www.google.com/search?ei=j-iKXNDxDMPdwALdwofACg&q='


com = 'siemens'

#for url in search(com, tld='de', lang='de', stop=10):
#    print(url)

response = requests.get(url+com)
soup = BS(response.content, 'html.parser')

红色标记区域是信息栏

【问题讨论】:

    标签: python beautifulsoup request


    【解决方案1】:

    您可以使用 BeautifuLSoup 中的查找功能来检索具有给定类名、id、css 选择器、xpath 等的所有元素。如果您检查信息栏(右键单击它并给出“检查”),您可以找到该栏的唯一类名或 ID。使用它从 BeautifulSoup 解析的整个 html 中单独过滤信息栏。

    查看 BeautifulSoup 中的 find() 和 findall() 以实现您的输出。始终首先通过 id 查找,因为每个 id 对 html 元素都是唯一的。如果没有相应的 id,则选择其他选项。

    要获取 URL,请将 google.com/search?q=[] 与 [] 内的搜索查询结合使用。对于包含多个单词的查询,请在中间使用“+”

    【讨论】:

    • 但是您需要 url 才能进入谷歌显示所有结果的特定页面。我没有那个特定页面的网址。我正在为“Googlesearch”包使用“搜索”。
    • 您可以使用 google.com/search?q=[ ]。在括号内提供您的搜索词。使用 + 分隔单词。例如:如果您的查询是“年度最佳电影”,您需要给出 q=Top+movies+of+the+year
    【解决方案2】:

    确保您使用user-agent 伪造真实用户访问,否则可能会导致来自 Google 的请求被阻止。 List 的用户代理。

    要从页面中直观地选择元素,您可以使用SelectorGadgets Chrome 扩展程序来抓取 CSS 选择器。

    代码和example in online IDE

    from bs4 import BeautifulSoup
    import requests, lxml
    
    headers = {
        "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19582"
    }
    
    response = requests.get('https://www.google.com/search?q=simens', headers=headers).text
    soup = BeautifulSoup(response, 'lxml')
    
    title = soup.select_one('.SPZz6b h2').text
    subtitle = soup.select_one('.wwUB2c span').text
    website = soup.select_one('.ellip .ellip').text
    snippet = soup.select_one('.Uo8X3b+ span').text
    print(f'{title}\n{subtitle}\n{website}\n{snippet}')
    

    输出:

    Siemens
    Automation company
    siemens.com
    Siemens AG is a German multinational conglomerate company headquartered in Munich and the largest industrial manufacturing company in Europe with branch offices abroad.
    

    或者,您可以使用来自 SerpApi 的 Google Search Engine Results API。这是一个带有免费计划的付费 API。

    要集成的代码:

    import os
    from serpapi import GoogleSearch
    
    params = {
        "engine": "google",
        "q": "simens",
        "api_key": os.getenv("API_KEY"),
    }
    
    search = GoogleSearch(params)
    results = search.get_dict()
    
    title = results["knowledge_graph"]["title"]
    subtitle = results["knowledge_graph"]["type"]
    website = results["knowledge_graph"]["website"]
    snippet = results["knowledge_graph"]["description"]
    print(f'{title}\n{subtitle}\n{website}\n{snippet}')
    

    输出:

    Siemens
    Automation company
    http://www.siemens.com/
    Siemens AG is a German multinational conglomerate company headquartered in Munich and the largest industrial manufacturing company in Europe with branch offices abroad.
    

    免责声明,我在 SerpApi 工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多