用python请求库进行google搜索 [英] google search with python requests library

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

问题描述

(我尝试过查找,但所有其他答案似乎都在使用urllib2)

(I've tried looking but all of the other answers seem to be using urllib2)

我刚刚开始尝试使用请求,但是对于如何发送或请求页面中的其他内容,我仍然不太清楚.例如,我将拥有

I've just started trying to use requests, but I'm still not very clear on how to send or request something additional from the page. For example, I'll have

import requests

r = requests.get('http://google.com')

但是我不知道现在该如何做,例如,使用显示的搜索栏进行谷歌搜索.我已经阅读了快速入门指南,但是我对HTML POST等不是很熟悉,因此它并不是很有帮助.

but I have no idea how to now, for example, do a google search using the search bar presented. I've read the quickstart guide but I'm not very familiar with HTML POST and the like, so it hasn't been very helpful.

是否有一种干净优雅的方法来完成我要问的事情?

Is there a clean and elegant way to do what I am asking?

推荐答案

请求概述

Google搜索请求是标准HTTP GET命令.它包含与您的查询相关的参数的集合.这些参数以名称=值对的形式包含在请求URL中,并以与号(&)字符分隔.参数包括搜索查询之类的数据以及唯一的CSE ID(cx),该ID标识正在发出HTTP请求的CSE. WebSearch或图像搜索服务返回XML结果以响应您的HTTP请求.

The Google search request is a standard HTTP GET command. It includes a collection of parameters relevant to your queries. These parameters are included in the request URL as name=value pairs separated by ampersand (&) characters. Parameters include data like the search query and a unique CSE ID (cx) that identifies the CSE that is making the HTTP request. The WebSearch or Image Search service returns XML results in response to your HTTP requests.

首先,您必须在自定义搜索引擎的控制面板中获取CSE ID(cx参数)

然后,请访问Google Developers官方网站进行自定义搜索.

有很多这样的例子:

http://www.google.com/search?
  start=0
  &num=10
  &q=red+sox
  &cr=countryCA
  &lr=lang_fr
  &client=google-csbe
  &output=xml_no_dtd
  &cx=00255077836266642015:u-scht7a-8i

并解释了您可以使用的参数列表.

And there are explained the list of parameters that you can use.

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

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