我如何获得“反馈"的内容? Google搜索中的“框"? [英] How can I get the contents of the "feedback" box from Google searches?

查看:98
本文介绍了我如何获得“反馈"的内容? Google搜索中的“框"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您在Google搜索中提出问题或要求对单词进行定义时,Google会在反馈" 框中为您提供答案的摘要.

When you ask a question or request the definition of a word in a Google search, Google gives you a summary of the answer in the "feedback" box.

例如,当您搜索define apple时,您会得到以下结果:

For example, when you search for define apple you get this result:

现在,我想明确地说,我不需要不需要整个页面或其他结果,我只需要此框即可:

Now, I would like to make it clear that I do not need the entire page or the other results, I just need this box:

如何使用RequestsBeautiful Soup模块获取Python 3中此"feedback" 框的内容?

How can I use the Requests and Beautiful Soup modules to get the contents of this "feedback" box in Python 3?

如果无法实现,我可以使用Google搜索API来获取反馈" 框中的内容吗?

If that is not possible can I use the Google Search Api to get the contents of the "feedback" box?

我在SO上找到了一个类似问题,但是OP未指定语言,没有答案,我担心这两个评论已过时,因为这个问题是在将近9个月前提出的.

I have found a similar question on SO but the OP has not specified the language, there are no answers and I fear that the two comments are outdated as this question was asked nearly 9 months ago.

感谢您的时间&提前帮助.

Thank you for your time & help in advance.

推荐答案

问题是个好主意

程序可以用以下命令启动 python3 defineterm.py苹果

program can be started with python3 defineterm.py apple

#! /usr/bin/env python3.5
# defineterm.py

import requests
from bs4 import BeautifulSoup
import sys
import html
import codecs

searchterm = ' '.join(sys.argv[1:])

url = 'https://www.google.com/search?q=define+' + searchterm
res = requests.get(url)
try:
    res.raise_for_status()
except Exception as exc:
    print('error while loading page occured: ' + str(exc))

text = html.unescape(res.text)
soup = BeautifulSoup(text, 'lxml')
prettytext = soup.prettify()

#next lines are for analysis (saving raw page), you can comment them
frawpage = codecs.open('rawpage.txt', 'w', 'utf-8')
frawpage.write(prettytext)
frawpage.close()

firsttag = soup.find('h3', class_="r")
if firsttag != None:
    print(firsttag.getText())
    print()

#second tag may be changed, so check it if not returns correct result. That might be situation for all searched tags.
secondtag = soup.find('div', {'style': 'color:#666;padding:5px 0'})
if secondtag != None:
    print(secondtag.getText())
    print()

termtags = soup.findAll("li", {"style" : "list-style-type:decimal"})

count = 0
for tag in termtags:
    count += 1
    print( str(count)+'. ' + tag.getText())
    print()

将脚本作为可执行文件

然后在〜/.bashrc中
可以添加此行

then in ~/.bashrc
this line can be added

alias defterm="/data/Scrape/google/defineterm.py "

输入正确的路径来编写位置脚本

putting correct path to script your place

然后执行

source ~/.bashrc

程序可以通过以下方式启动:

program can be started with:

defterm apple (or other term)

这篇关于我如何获得“反馈"的内容? Google搜索中的“框"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆