GoodZhang在学Python(十四)--Python3中urllib库常用操作

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

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

error:

# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------

import urllib.request
url1 = 'http://www.baidu.com/'
url2 = 'http://127.0.0.1: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.request

top_url = 'http://localhost:8080/test/index.jsp'
# 创建密码管理器
passwordmanager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
# 添加用户名及密码
passwordmanager.add_password(None, top_url, 'abc', '123')

handler = urllib.request.HTTPBasicAuthHandler(passwordmanager)

# create opener
opener = urllib.request.build_opener(handler)

# use the opener to fetch a URL
low_url = 'http://localhost:8080/test/index.jsp'
x = opener.open(low_url)
print(x.read())

# install the opener, all calls will use our opener to open
urllib.request.install_opener(opener)

response = urllib.request.urlopen(low_url)
html = response.read()
print(html.decode('utf-8'))


代理:

# ----------------------------------
__author__ = 'zWX231671'
__description__ = ''
# ----------------------------------
import urllib.request
url = 'http://localhost: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 socket
import urllib.request

url1 = 'http://localhost:8080/test/index.jsp'
url2 = 'http://www.baidu.com/'
timeout = 2
socket.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.parse
import urllib.request

url = 'http://127.0.0.1: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 data
data = 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'))



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值