2015-10-20 72 views
-1

我應該使用urllib庫編寫一個函數來打開一個url然後讀取並解碼該文件並返回字符串表單。到目前爲止,我有在python中讀取urllib

def request(url): 
    urllib.request.Request(url) 
    #open URL 
    urllib.request.urlopen(url) 
    #read URL and decode the content string 

    #return string form of URL content 

我真的不知道如何讀取和解碼信息,一旦我下載它。如果有人能幫助我。因爲它讀取IM assumsing您將使用

urllib.request.read(url) 

,但我不知道,我不知道會出現什麼文件回來,我應該如何進行解碼。

回答

0
>>> from urllib import request 
>>> data = request.urlopen("http://www.google.com").read() 

通過對request.urlopen調用read()功能你會得到你傳遞的URL的source