使用GET方法在google中搜索

2024-05-15 02:50:27 发布

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

我想用GET方法查询google并检索HTML结果。我试过以下方法:

import httplib
import urllib

params = urllib.urlencode({'gws_rd': 'ssl', 'q': 'hello'})  # search for hello
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
c = httplib.HTTPSConnection("google.com")
c.request("GET", "/", params, headers)
response = c.getresponse()
print response.status, response.reason
data = response.read()
file_ = open('result.html', 'w')
file_.write(data)
file_.close()

但我得到一个错误:

400 Bad Request

有人知道我怎么解决这个问题吗?在


Tags: 方法importhellodatagetresponsehtmlgoogle
1条回答
网友
1楼 · 发布于 2024-05-15 02:50:27

嗨,你想做的已经过时了。您可以查看下面的链接以获取参考。在

https://developers.google.com/web-search/

取而代之的是谷歌提供了新的API。在

https://developers.google.com/custom-search/json-api/v1/overview

上面的链接将为您提供如何使用它的完整信息。在

你必须创建一个密钥,只需稍微修改一下代码就可以了。在

相关问题 更多 >

    热门问题