用Python在Google中搜索

2024-03-29 14:53:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用python脚本在Google中搜索一个文本,并返回每个结果的名称、描述和URL。我当前正在使用以下代码:

from google import search

ip=raw_input("What would you like to search for? ")

for url in search(ip, stop=20):
     print(url)

这只返回URL。如何返回每个URL的名称和说明?


Tags: 代码from文本importip脚本名称url
3条回答

我假设您使用this library by Mario Vilas是因为他的代码中出现了stop=20参数。似乎这个库除了url之外什么都不能返回,这使它非常不发达。因此,您要对当前使用的库执行的操作是不可能的。

我建议你用abenassi/Google-Search-API。然后你可以简单地:

from google import google
num_page = 3
search_results = google.search("This is my query", num_page)
for result in search_results:
    print(result.description)

我试过使用它们,但大多数都没有成功,或者给出了一些错误,比如尽管导入了包,但仍然找不到搜索模块。或者我确实使用了selenium web驱动程序,如果与Firefox、chrome、Phantom web browser一起使用,效果会很好,但在执行时间上还是有点慢,因为它先查询浏览器,然后返回搜索结果。

所以我想到了使用google api,它的工作速度惊人地快,返回的结果也非常准确。

在我分享代码之前,这里有几个简单的提示:

  1. 在Google Api上注册以获取Google Api密钥(免费版本)
  2. 现在搜索Google自定义搜索并设置您的免费帐户以获取自定义搜索id
  3. 现在将此包(google api python客户端)添加到您的python项目中 (可以通过写作来完成!pip安装google api python客户端)

就这样,你现在要做的就是运行以下代码:

from googleapiclient.discovery import build

my_api_key = "your API KEY TYPE HERE"
my_cse_id = "YOUR CUSTOM SEARCH ENGINE ID TYPE HERE"

def google_search(search_term, api_key, cse_id, **kwargs):
      service = build("customsearch", "v1", developerKey=api_key)
      res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
      return res['items']

results= google_search("YOUR SEARCH QUERY HERE",my_api_key,my_cse_id,num=10) 

for result in results:
      print(result["link"])

我并没有详细说明我在寻找什么,但我现在发现自己是一个很好的解决方案(如果我能使这个更好的话,我可能会编辑这个)。我像以前一样在Google中搜索(只返回URL)和用于解析HTML页面的漂亮的Soup包:

from google import search
import urllib
from bs4 import BeautifulSoup

def google_scrape(url):
    thepage = urllib.urlopen(url)
    soup = BeautifulSoup(thepage, "html.parser")
    return soup.title.text

i = 1
query = 'search this'
for url in search(query, stop=10):
    a = google_scrape(url)
    print str(i) + ". " + a
    print url
    print " "
    i += 1

这给了我一个页面标题和链接的列表。

还有另一个很好的解决方案:

from google import search
import requests

for url in search(ip, stop=10):
            r = requests.get(url)
            title = everything_between(r.text, '<title>', '</title>')

相关问题 更多 >