在JetBrains PyCharm 2016.3软件中编写代码前,需要指定python和编码方式:#!user/bin/python
编码方式 :#coding=utf-8或者#--coding:utf-8--
post请求:
导入工具,内置的库
import urllib
import urllib2
加一个\可以换行
response = \
#urllib2.urlopen("https://hao.360.cn/?wd_xp1")
print response.read()
request = urllib2.Request('http://www.baidu.com')
response = urllib2.urlopen(request)
构造post请求
params={}
params['account']='jredu'
params['pwd']=''
对数据进行编码
data = urllib.urlencode(params)
response = urllib2.urlopen(request,data)
print response.url
print response.code
get请求:
导入工具,内置的库
import urllib
import urllib2
加一个\可以换行
response = \
#urllib2.urlopen("https://hao.360.cn/?wd_xp1")
print response.read()
url='http://www.baidu.com'
response = urllib2.urlopen(request)
构造post请求
params={}
params['account']='jredu'
params['pwd']=''
对数据进行编码
data = urllib.urlencode(params)
request = urllib2.Request(url+"?"+data)
response = urllib2.urlopen(request)
print response.url
print response.code
print response.read()
原文链接:https://blog.csdn.net/xuehyunyu/article/details/74549850