GoodZhang的的专栏

在Python3中,整合了原来的urllib与urllib2,有一些常用操作也是有变化,,在学习的过程中,找到的很多例子都是在python2上运行的,很忧桑。。。

下面是写的一些python3中常用操作的代码:

error:# ———————————-__author__ = 'zWX231671'__description__ = ''# ———————————-import urllib.requesturl1 = ''url2 = ':8080/test/index.jsp'req = urllib.request.Request(url2)try:response = urllib.request.urlopen(req)except urllib.error.HTTPError as e:print(e.code)print('the server couldn\&;t fulfill the request')except urllib.error.URLError as e:print(e.code)print('failed to reach a server')else:print('good job!')print(response.read().decode('utf-8'))认证:# ———————————-__author__ = 'zWX231671'__description__ = ''# ———————————-import urllib.requesttop_url = ':8080/test/index.jsp'# 创建密码管理器passwordmanager = urllib.request.HTTPPasswordMgrWithDefaultRealm()# 添加用户名及密码passwordmanager.add_password(None, top_url, 'abc', '123')handler = urllib.request.HTTPBasicAuthHandler(passwordmanager)# create openeropener = urllib.request.build_opener(handler)# use the opener to fetch a URLlow_url = ':8080/test/index.jsp'x = opener.open(low_url)print(x.read())# install the opener, all calls will use our opener to openurllib.request.install_opener(opener)response = urllib.request.urlopen(low_url)html = response.read()print(html.decode('utf-8'))

代理:# ———————————-__author__ = 'zWX231671'__description__ = ''# ———————————-import urllib.requesturl = ':8080/test/index.jsp'proxy = urllib.request.ProxyHandler({'sock5': 'localhost:8090'})opener = urllib.request.build_opener(proxy)urllib.request.install_opener(opener)response = urllib.request.urlopen(url)html = response.read()print(html.decode('utf-8'))timeout:# ———————————-__author__ = 'zWX231671'__description__ = ''# ———————————-import socketimport urllib.requesturl1 = ':8080/test/index.jsp'url2 = ''timeout = 2socket.setdefaulttimeout(timeout)req = urllib.request.Request(url2)response = urllib.request.urlopen(req)html = response.read()print(html.decode('utf-8'))request header设置:# ———————————-__author__ = 'zWX231671'__description__ = ''# ———————————-import urllib.parseimport urllib.requesturl = ':8080/test/index.jsp'user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'values = {'name': 'abc','password': '123'}data = urllib.parse.urlencode(values)# that params output from urlencode is encoded to bytes before it is sent to urlopen as datadata = data.encode('utf-8')headers = {'User-Agent': user_agent}req = urllib.request.Request(url, data, headers)response = urllib.request.urlopen(req)html = response.read()print(html.decode('utf-8'))

如果心在远方,只需勇敢前行,梦想自会引路,

GoodZhang的的专栏

相关文章:

你感兴趣的文章:

标签云: