Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python请求库进行google搜索_Python_Python Requests_Google Search_Google Search Api - Fatal编程技术网

使用python请求库进行google搜索

使用python请求库进行google搜索,python,python-requests,google-search,google-search-api,Python,Python Requests,Google Search,Google Search Api,(我试过查找,但所有其他答案似乎都使用urllib2) 我刚刚开始尝试使用请求,但我仍然不太清楚如何从页面发送或请求其他内容。比如说,我要 import requests r = requests.get('http://google.com') 但是我现在不知道怎么做,比如说,使用上面的搜索栏进行谷歌搜索。我读过《快速入门指南》,但我对HTML帖子之类的东西不太熟悉,所以它没有什么帮助 有没有一种干净优雅的方式来满足我的要求?请求概述 Google搜索请求是一个标准的HTTP GET命令。

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

我刚刚开始尝试使用请求,但我仍然不太清楚如何从页面发送或请求其他内容。比如说,我要

import requests

r = requests.get('http://google.com')
但是我现在不知道怎么做,比如说,使用上面的搜索栏进行谷歌搜索。我读过《快速入门指南》,但我对HTML帖子之类的东西不太熟悉,所以它没有什么帮助

有没有一种干净优雅的方式来满足我的要求?

请求概述

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

首先,必须在以下位置获取CSE ID(cx参数)

那么

有很多这样的例子:

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

这里还解释了您可以使用的参数列表。

输入:

import requests

def googleSearch(query):
    with requests.session() as c:
        url = 'https://www.google.co.in'
        query = {'q': query}
        urllink = requests.get(url, params=query)
        print urllink.url

googleSearch('Linkin Park')
https://www.google.co.in/?q=Linkin+Park
输出:

import requests

def googleSearch(query):
    with requests.session() as c:
        url = 'https://www.google.co.in'
        query = {'q': query}
        urllink = requests.get(url, params=query)
        print urllink.url

googleSearch('Linkin Park')
https://www.google.co.in/?q=Linkin+Park

将返回{'text':text,'url':url}格式的google结果数组。顶级结果url将是
google('search query')[0]['url']

您可以在不使用客户端库的情况下使用谷歌API。我在Python3中使用带有urllib.request模块的GoogleDrive。我的意思不仅仅是在Google的上下文中,我还希望能够搜索其他网站/数据库。另外,我认为现在的标准是请求模块,因为urllib/urllib2已经变得笨重/过时了?一些方法(GET)通过url传递参数,另一些方法(POST)通过数据传递参数。而且两者都允许标题(成对或关键字和值)仅供参考,自动化脚本搜索与谷歌的TOS背道而驰-你应该改用谷歌的定制搜索API()。更干净,无需与BeautifulSoup合作。