python3 get和post

 

 

post:

from urllib.error import URLError,HTTPError  
import urllib.request  
import urllib.parse  
url='http://www.wunderground.com/cgi-bin/findweather/getForecast'  
values={'query':'10001'}  
url_values=urllib.parse.urlencode(values)  
    #print(url_values)        
url_values=url_values.encode(encoding='UTF8')  
full_url=urllib.request.Request(url,url_values)
try:  
    response=urllib.request.urlopen(full_url)   #open=urlopen
    data=response.read()
    print(data)
except HTTPError as e:  
    print('Error code:',e.code)   
except URLError as e:  
    print('Reason',e.reason)  
    the_page=response.read()  
    print(the_page)

 

get:

from urllib.error import URLError,HTTPError  
import urllib.request  
import urllib.parse  
url='http://www.wunderground.com/cgi-bin/findweather/getForecast'  
values={'query':'10001'}  
url_values=urllib.parse.urlencode(values)
newUrl=url+'?'+url_values
print(newUrl)
try:  
    response=urllib.request.urlopen(newUrl)   #open=urlopen
    data=response.read()
    print(data)
except HTTPError as e:  
    print('Error code:',e.code)   
except URLError as e:  
    print('Reason',e.reason)  
    the_page=response.read()  
    print(the_page)
posted @ 2014-06-11 16:38  sxcww  阅读(465)  评论(0编辑  收藏  举报