Python 官方文档:入门教程 => 点击学习
目录一、简介二、发起请求三、携带参数请求四、获取响应数据五、设置headers六、使用代理七、认证登录八、设置cookie九、异常处理十、Http异常十一、超时异常十二、解析编码十三
import urllib.request
# 方法一
resp = urllib.request.urlopen('http://www.baidu.com', timeout=1)
print(resp.read().decode('utf-8'))
# 方法二
request = urllib.request.Request('http://www.baidu.com')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
import urllib.parse
import urllib.request
params = {
'name':'autofelix',
'age':'25'
}
data = bytes(urllib.parse.urlencode(params), encoding='utf8')
response = urllib.request.urlopen("http://www.baidu.com/", data=data)
print(response.read().decode('utf-8'))
import urllib.request
resp = urllib.request.urlopen('http://www.baidu.com')
print(type(resp))
print(resp.status)
print(resp.geturl())
print(resp.getcode())
print(resp.info())
print(resp.getheaders())
print(resp.getheader('Server'))
import urllib.request
headers = {
'User-Agent': 'Mozilla/5.0 (windows NT 6.1; Win64; x64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}
request = urllib.request.Request(url="http://tieba.baidu.com/", headers=headers)
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
import urllib.request
proxys = urllib.request.ProxyHandler({
'http': 'proxy.cn:8080',
'https': 'proxy.cn:8080'
})
opener = urllib.request.build_opener(proxys)
urllib.request.install_opener(opener)
request = urllib.request.Request(url="http://www.baidu.com/")
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))
import urllib.request
url = "http://www.baidu.com/"
user = 'autofelix'
passWord = '123456'
pwdmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
pwdmgr.add_password(None,url,user,password)
auth_handler = urllib.request.HTTPBasicAuthHandler(pwdmgr)
opener = urllib.request.build_opener(auth_handler)
response = opener.open(url)
print(response.read().decode('utf-8'))
Cookies
来自动登录,免去重复登录验证的操作import http.cookiejar
import urllib.request
cookie = http.cookiejar.CookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
response = opener.open("http://www.baidu.com/")
f = open('cookie.txt', 'a')
for item in cookie:
f.write(item.name+" = "+item.value+'\n')
f.close()
from urllib import error, request
try:
resp = request.urlopen('http://www.baidu.com')
except error.URLError as e:
print(e.reason)
from urllib import error, request
try:
resp = request.urlopen('http://www.baidu.com')
except error.HTTPError as e:
print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e:
print(e.reason)
else:
print('request successfully')
import Socket, urllib.request, urllib.error
try:
resp = urllib.request.urlopen('http://www.baidu.com', timeout=0.01)
except urllib.error.URLError as e:
print(type(e.reason))
if isinstance(e.reason,socket.timeout):
print('time out')
from urllib import parse
name = parse.quote('飞兔小哥')
# 转换回来
parse.unquote(name)
from urllib import parse
params = {'name': '飞兔', 'age': '27', 'height': '178'}
parse.urlencode(params)
from urllib.parse import urlparse
result = urlparse('http://www.baidu.com/index.html?user=autofelix')
print(type(result))
print(result)
from urllib.parse import urljoin
print(urljoin('http://www.baidu.com', 'index.html'))
from urllib.parse import urlencode
params = {
'name': 'autofelix',
'age': 27
}
baseUrl = 'http://www.baidu.com?'
print(baseUrl + urlencode(params))
到此这篇关于python 包中的 urllib 网络请求教程的文章就介绍到这了,更多相关 urllib 网络请求 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: python 包实现 urllib 网络请求操作
本文链接: https://lsjlt.com/news/117424.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0