1

Google検索で質問をしたり、単語の定義をリクエストすると、"feedback"ボックスに回答の概要が表示されます。Googleの検索から「フィードバック」ボックスの内容を取得するにはどうすればよいですか?

は、たとえば、あなたがdefine appleを検索するときに、このような結果を得る:今

example of feedback

を、私はそれを明確に私はは、ページ全体またはその他の結果を必要としないことをしたいと思い、

highlighted example of feedback

どのように私はRequestsとを使用することができます。私は、このボックスを必要としますPython 3の"feedback"ボックスの内容を取得するモジュールはありますか?

Googleの検索APIを使用しての「フィードバック」ボックスの内容を取得できますか?

私はsimilar questionを見つけましたが、OPには言語が指定されていません。回答はありません。この質問はほぼ9ヶ月前に尋ねられたので、2つのコメントが古くなってしまう恐れがあります。

ありがとうございました&助けてください。

答えて

0

質問は、〜/ .bashrcの
にこの行をすることができ、実行可能

としてメイクスクリプト

プログラムは のpython3のdefineterm.pyリンゴを開始することができ

#! /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() 

いいアイデアです追加

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

その後、

実行するあなたの場所のスクリプトに正しいパスを入れ

source ~/.bashrc 

プログラムが使用を開始することができます:あなただけの

defterm apple (or other term) 
+0

ありがとうございましたので、答えとも 'deftermのために非常に多くの'スクリプト。私は今それをテストしています。私は毎日の投票限度額を使いましたが、私はあなたにふさわしい投票をするために2時間後に戻ってきます:Dまた、私は 'defterm'スクリプトを修正してGithubに再度載せることを許可しますありがとうございました。 –

+0

Pythonスクリプトとdeftermの問題はありません。 – josifoski

1

それは簡単要求BS4を使用して行われ、 のテキストを引き出す必要があります。lr_dct_ent

import requests 
from bs4 import BeautifulSoup 

h = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36"} 
r = requests.get("https://www.google.ie/search?q=define+apple", headers=h).text 
soup = BeautifulSoup(r) 

print("\n".join(soup.select_one("div.lr_dct_ent").text.split(";"))) 

メインテキストは、名詞がlr_dct_sf_hクラスとdiv要素である、順序付きリストである:

In [11]: r = requests.get("https://www.google.ie/search?q=define+apple", headers=h).text 
In [12]: soup = BeautifulSoup(r,"lxml")  
In [13]: div = soup.select_one("div.lr_dct_ent")  
In [14]: n_v = div.select_one("div.lr_dct_sf_h").text 
In [15]: expl = [li.text for li in div.select("ol.lr_dct_sf_sens li")]  
In [16]: print(n_v) 
noun 

In [17]: print("\n".join(expl)) 
1. the round fruit of a tree of the rose family, which typically has thin green or red skin and crisp flesh.used in names of unrelated fruits or other plant growths that resemble apples in some way, e.g. custard apple, oak apple. 
used in names of unrelated fruits or other plant growths that resemble apples in some way, e.g. custard apple, oak apple. 
2. the tree bearing apples, with hard pale timber that is used in carpentry and to smoke food. 
+0

ありがとうございます。私は今それをテストしています。私は毎日の投票限度額を使用しましたが、あなたにふさわしい投票をするために2時間後に戻ってきます:D –

+0

心配する必要はありません –

関連する問題