在Python 2.7中显示Google的搜索结果

时间:2016-04-16 05:35:09

标签: python python-2.7 search google-search

我想知道是否有办法在我的Python程序中将Google的搜索结果显示为输出。就像我在程序中输入“电力”一样,我希望将Google的搜索结果显示为纯文本。有办法吗?

更新

import urllib2

response = urllib2.urlopen("https://en.wikipedia.org/wiki/Machine_learning")
the_page = response.read(bytes)
content = str(the_page)
print the_page

我尝试了上面的代码,但是如果我输入

,它会显示错误
the_page = response.read()
print the_page

它只是打印页面的HTML格式而不是文本字符串,所以如何单独获取字符串?

1 个答案:

答案 0 :(得分:0)

import urllib2

response = urllib2.urlopen("https://en.wikipedia.org/wiki/Machine_learning")
content= response.read()
# Now it's time for parsing *content* to extract the relevant data
# Use regex, HTMLParser from standard library
# or use beautifulsoup, LXML (third party)