2017-02-06 3 views
-3

これはコマンド(変数com)を入力するプログラムの一部です。コマンドにgoogleや検索が含まれていれば、Google検索が実行されます。 誰かがそれを修正して、googleを含む検索(たとえば、「Google googleがお金を稼ぐ方法」など)に対応できるようにすることはできますか?Google検索を実行するコードが改善する必要があります

if 'google' in com or 'search' in com: 
    search='' 
    foo=com.split()   
    for x in foo: 
     if x!='google' and x!='search': 
      search+='+'+x 
    search=search[1:] 
    print('googling:'+search) 
    time.sleep(1) 
    web.open('https://www.google.co.in/search?q='+search+'&oq='+search+'&aqs=chrome..69i57j69i60l2j69i61j69i60j69i61.306j0j9&s0ourceid=chrome&ie=UTF-8') 

答えて

0

あなたはgoogleまたはsearchある検索要求、彼らはあなたのcom変数に配置されているにかかわらずからのすべての部分を削除しています。その後、ある

com = 'google how google makes money' 
url = 'https://www.google.com/search?q={query:s}' 
pts = com.split() 
cmd, qry = pts[0], ' '.join(pts[1:]) 

print(cmd) 
>>> 'google' 
print(qry) 
>>> 'how google makes money' 

はちょうどあなたが(cmdの最初の単語だけ)と要求(cmdの残りの言葉)コマンドを保持するように、first:restに分割しましたその後、通常どおりクエリを実行できます。ところで:

import requests 
from bs4 import BeautifulSoup 

if cmd in ['google', 'search']: 
    res = requests.get(url.format(query=qry)) 
    sup = BeautifulSoup(res.text, 'html.parser') 

    res_root = sup.find('div', id='ires') 
    for entry in res_root.find_all('h3'): 
     print(entry.text) 

収量:

How Does Google Make Its Money? | Investopedia 
The Business of Google (GOOG) | Investopedia 
BBC iWonder - How does Google make money? 
How does Google earn money? - Quora 
How Google Makes Money? - Revenues & Profits 
How Does Google Make Money? - Minterest 
If Google is free, how does it make so much money? – Channel 4 ... 
Four ways Google makes money outside of advertising - TechRepublic 
Google - How (precisely) it profits from YouTube - STL Partners ... 
How Google's Money-Making Machine, AdWords, Actually Works ... 
あなたがあなたのブラウザからコピーしたすべてのトークンを含む完全なURLを指定する必要はありません
関連する問題