Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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搜索引擎错误_Python_Python 2.7 - Fatal编程技术网

访问代码时出现Python搜索引擎错误

访问代码时出现Python搜索引擎错误,python,python-2.7,Python,Python 2.7,这是我正在使用的代码 from google import google search_results = google.search("this is test", 3) for result in search_results: print(result.description) 现在,当我试图访问它时,我得到了这个错误 Error accessing: http://www.google.com/search?q= 有什么问题吗 编辑:我不是问替代方案,我只是问为什么会出现

这是我正在使用的代码

from google import google

search_results = google.search("this is test", 3)
for result in search_results:
    print(result.description)
现在,当我试图访问它时,我得到了这个错误

 Error accessing: http://www.google.com/search?q=
有什么问题吗


编辑:我不是问替代方案,我只是问为什么会出现这个问题?解决方案是什么?

您可以使用请求python库在google上搜索。通过pip安装请求您可以使用google搜索任何内容,并使用Beautifulsoup解析结果。下面的代码在google上搜索查询,然后使用BeautifulSoup获取google返回的URL

import requests
import urllib
from bs4 import BeautifulSoup

query = 'any search term'


r = requests.get('https://www.google.com/search?q={}'.format(query))
soup = BeautifulSoup(r.text, "html.parser")
links = []
for item in soup.find_all('h3', attrs={'class' : 'r'}):
    links.append(item.a['href'])

print(links)

嗨,Himanshu,我之前也这么做了,但我的一些前辈告诉我,这不是一个好方法,他们还告诉我,你应该创建一个自定义搜索引擎来实现这一点从谷歌导入谷歌是一个定制引擎?你所说的定制引擎是什么意思?一般来说,主要原则是不要重新发明轮子。我认为@Himanshu解决方案是好的。否则,请准确地指定您需要的内容。您好,@TomWojcik,我是指使用谷歌API的谷歌定制引擎。你们两个都没告诉我为什么会出现上述错误。?