Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.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 - Fatal编程技术网

在Python中,如何从括号和大括号中获取数据?

在Python中,如何从括号和大括号中获取数据?,python,Python,我正在尝试使用py网络搜索模块从谷歌搜索中提取信息。拉动搜索的结果如下(如中所述): 我似乎不知道如何打印“链接”数据 result=Google.search(query='hello world', num=5, start=0, country_code="es") data=result['results'] print(data) 这是我的测试代码,它只打印[]。有什么建议吗?您所做的是正确的,但该库已过时,不会返回任何谷歌搜索结果。详情请参阅: 此外,谷歌还改变了HTML结构。因此

我正在尝试使用py网络搜索模块从谷歌搜索中提取信息。拉动搜索的结果如下(如中所述):

我似乎不知道如何打印“链接”数据

result=Google.search(query='hello world', num=5, start=0, country_code="es")
data=result['results']
print(data)

这是我的测试代码,它只打印
[]
。有什么建议吗?

您所做的是正确的,但该库已过时,不会返回任何谷歌搜索结果。详情请参阅:

此外,谷歌还改变了HTML结构。因此,必须更新刮板。谢谢你的错误报告

作为一个临时解决方案,您可以考虑使用Bing作为您的搜索引擎,因为它仍然有效:

from pws import Bing
result = Bing.search('hello world', 5, 2)
print(result["results"])

列表(方括号)仅由数字索引,从0开始。字典(大括号)通常通过点符号或方括号编制索引,但带有字符串。这有帮助吗?看起来您的尝试还可以,但我建议您尝试打印result变量,看看字典中的results条目中是否有任何结果。数据是json格式的。使用python中的json模块提取数据或将数据转储到字典中。人们,这不是json。这是一个python字典,显然不是JSON,因为它有单引号;JSON对这些东西非常严格,只有双引号是有效的。刚刚测试过它,这正是我看到的<代码>{'search\u engine':'google','received\u num':0','expected\u num':10','country\u code':None','start':0',url':'https://www.google.com/search?q=hello+world&num=10&start=0','related_querys':[“hello world歌词”、“hello world html”、“hello world歌曲”、“hello world python”、“hello world java”、“hello world book”、“hello world c++”、“hello world教程”]、“hello world教程”]、“总计结果”:35100000,“结果”:[]]@J.B确切地说,
results
列表是空的。谢谢各位。我想我在那里做错了什么。你们知道有任何库可以用于此目的吗?是的,试着看看(第119行),在那里解析结果:
raw\u results=soup.find\u all('li',attrs={class':'g'))
。换句话说,它基于'li'标记,但如果你查看google结果的HTML代码,那里没有li标记。
from pws import Bing
result = Bing.search('hello world', 5, 2)
print(result["results"])