用Python抓取Google快速回答框

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

我对 Python 编程非常陌生,我正在尝试制作一个简单的应用程序。

我想做的是在谷歌上搜索文本并返回链接,我的程序做得很好。另一件事是,如果谷歌有如下图所示的快速答案,我想抓住它,这就是我的问题所在。我尝试在网上搜索,发现很少有代码无法工作的主题。

谷歌快速框答案:

通过检查许多页面的代码,我注意到答案总是在一个名为

_XWk
的类中,但是在 Python 中,当我获取页面的代码并搜索这个类时,它没有找到它。我在Python中尝试了很多方法来抓取页面,但它从来没有得到这个类,而且我认为它得到的代码比我打开页面源代码时浏览器向我显示的代码要少。

班级

_XWk


代码:

import requests, lxml
from bs4 import BeautifulSoup

url = 'https://www.google.com/search?q=when%20was%20trump%20born'
h = {"User-Agent":"Chrome/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"}

r = requests.get(url, headers=h).text
soup = BeautifulSoup(r,'lxml')

soup.find_all("div", class_="_xwk")
print (soup)

如有任何帮助,我们将不胜感激。

python python-3.x web-scraping beautifulsoup python-requests
4个回答
4
投票

soup.find_all("div", class_="_xwk")
在您的代码中没有任何作用。
find_all()
函数返回与给定参数匹配的标签列表。因此,您需要将此结果保存在变量中。

但是,由于您只需要一个这样的标签,因此可以使用

find()
,它返回第一个匹配的标签。

最后,要获取标签内的文本,您必须使用

.text
属性。

此外,类名区分大小写。在检查中,类名是

_XWk
而不是
_xwk
。进行这些更改,代码:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'}
r = requests.get('https://www.google.com/search?q=when%20was%20trump%20born', headers=headers)
soup = BeautifulSoup(r.text, 'lxml')

result = soup.find('div', class_='_XWk')
print(result.text)
# 14 June 1946 (age 71)

3
投票

为什么你看不到与浏览器中相同的结果的最常见问题是因为没有

user-agent
被传递到请求
headers
因此,当使用
user-agent
库时没有指定
requests
时,它默认为python-requests 并且 Google 知道它是一个机器人/脚本,然后它会阻止请求(或它所做的任何事情)。

这就是为什么您会收到具有不同 CSS 选择器的不同 HTML 和某种错误。检查您的用户代理是什么


如果您需要查找某个特定元素,您可以使用 SelectorGadget 查找

CSS
选择器并使用
select_one()
使其工作:

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"
}

html = requests.get('https://www.google.com/search?q=when trump is born', headers=headers).text

soup = BeautifulSoup(html, 'lxml')

born = soup.select_one('.XcVN5d').text
age = soup.select_one('.kZ91ed').text

print(born, age, sep='\n')

输出:

June 14, 1946
age 74 years

或者,您可以使用 SerpApi 中的 Google Direct Answer Box API 执行相同的操作。这是一个带有免费计划的付费 API。

最大的区别在于,您只需要关注要提取的数据,而不是弄清楚要使用哪些选择器以及如何绕过 Google 或其他搜索引擎的阻止。

要集成的代码:

from serpapi import GoogleSearch

params = {
  "api_key": "YOUR_API_KEY",
  "engine": "google",
  "q": "when trump is born",
  "google_domain": "google.com",
}

search = GoogleSearch(params)
results = search.get_dict()

date_born = results['answer_box']['answer']
print(date_born)

输出:

June 14, 1946

免责声明,我为 SerpApi 工作。


-2
投票

在全球范围内提供了约 3000 个就业机会后,我们的机构仍在全球范围内提供就业和商业贷款。

如果您需要工作或经济援助,请立即通过电子邮件与我们联系:[电子邮件受保护]

谢谢。


-3
投票

SerpApi尚不支持知识图谱直接答案。但您可以直接在您的案例中使用知识图:

$ curl https://serpapi.com/search.json?q=When+trum+was+born+?
...
  "knowledge_graph": {
    "title": "Donald Trump",
    "description": "Donald John Trump is the 45th and current President of the United States. Before entering politics, he was a businessman and television personality.\nTrump was born and raised in the New York City borough of Queens.",
    "source": {
      "name": "Wikipedia",
      "link": "https://en.wikipedia.org/wiki/Donald_Trump"
    },
    "born": "June 14, 1946 (age 72 years), Jamaica Hospital Medical Center, New York City, NY",
    "height": "6′ 3″ Trending",
    "full_name": "Donald John Trump",
    "net_worth": "3.1 billion USD (2019)",
    "parents": "Fred Trump, Mary Anne MacLeod Trump",
    "education": "Fordham University (1964–1966), New York Military Academy (1964), The Kew-Forest School"
  },
...
© www.soinside.com 2019 - 2024. All rights reserved.