使用 Python 进行 Google 搜索 [英] Google search with Python

查看:37
本文介绍了使用 Python 进行 Google 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何使用 Python 在 Google 上执行搜索查询?如何将搜索结果存储在 Microsoft Word 文档中?

How do you perform a search query on Google using Python? How do you store the search results in a Microsoft Word document?

推荐答案

使用 提供的 API.首先注册以获取 API 密钥此处.然后你可以使用 Python 的 urllib2 包来获取结果,例如

Use the provided API. First register to get an API key here. Then you can use Python's urllib2 package to fetch the results, e.g.

import urllib2
import json
import pprint
data = urllib2.urlopen('https://www.googleapis.com/customsearch/v1?key=YOUR_KEY_HERE&cx=017576662512468239146:omuauf_lfve&q=lectures')
data = json.load(data)
pprint.PrettyPrinter(indent=4).pprint(data['items'][0]) # Print the raw content of the first result

哪些输出

{   'cacheid': 'TxVqFzFZLOsJ',
    'displayLink': 'www.stanford.edu',
    'htmlSnippet': 'Apr 7, 2010 \u003cb\u003e...\u003c/b\u003e Course materials. \u003cb
u003eLecture\u003c/b\u003e slides xc2xb7 \u003cb\u003eLecture\u003c/b\u003e videos (2
008) xc2xb7 Review sessions. \u003cbr\u003e  Assignments. Homework xc2xb7 Reading. Exams
. Final exam \u003cb\u003e...\u003c/b\u003e',
    'htmlTitle': 'EE364a: \u003cb\u003eLecture\u003c/b\u003e Videos',
    'kind': 'customsearch#result',
    'link': 'http://www.stanford.edu/class/ee364a/videos.html',
    'snippet': 'Apr 7, 2010 ... Course materials. Lecture slides xc2xb7 Lecture videos (2008
) xc2xb7 Review sessions.   Assignments. Homework xc2xb7 Reading. Exams. Final exam ...',
        'title': 'EE364a: Lecture Videos'}

请确保将YOUR_KEY_HERE 替换为您的 密钥.

Please make sure to replace YOUR_KEY_HERE with your key.

要从 Python 创建 MS Word 文档,请阅读 this问题.

To create an MS Word document from Python, read this question.

这篇关于使用 Python 进行 Google 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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