2016-04-16 34 views
0

我想知道是否有方法在我的Python程序中顯示Google的搜索結果作爲輸出。就像我在我的程序中輸入「Electricity」一樣,我想將Google的搜索結果顯示爲純文本。有沒有辦法做到這一點?在Python 2.7中顯示Google的搜索結果

UPDATE

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格式而不是文本字符串,所以我如何獲得單獨的字符串?

+0

https://developers.google.com/custom-search/ – Natecat

+1

請提問有關您的代碼的問題,而不是一般的編碼問題。無論如何,我已經發布了一個答案。 –

+0

@CPanda看看我的更新,請幫助我.... –

回答

0
import urllib2 

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