Urllib库的部分使用
Urllib
:HTTP
请求库
4个模块
1 |
|
urlopen()
urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None)
- 将网页请求下来(
get请求,url里带参数
):import urllib.parse1
2
3
4
5
6import urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
print(response.read().decode('utf-8'))
```
2. 以`post`形式发送,`url`里不含参数,参数形成单独的字节发送
import urllib.request
source = bytes(urllib.parse.urlencode({‘word’:’hello’},encoding=’utf-8’))
response = urllib.request.urlopen(‘http://httpbin.org/post',source)
print (response.read())import urllib.requset1
3. 请求超时设置`timeout= `
response = urllib.request.urlopen(‘http://httpbin.org/get',timeout=1)
print(response.read())import socket1
4. 超时错误原因`socket.timeout`
import urllib.request
import urllib.error
try:
response = urllib.request.urlopen(‘http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e :
if isinstance(e.reason,socket.timeout):
print(“time out”)
1 |
|
import urllib.request
response = urllib.request.urlopen(‘http://httpbin.org')
print(type(response))
1 |
|
import urllib.request
response = urllib.request.urlopen(‘http://httpbin.org')
print (response.status)
print (response.getheaders())
print (response.getheader(‘Server’))
1 |
|
import request
response = urllib.request.urlopen(‘http://httpbin.org')
print(response.read().decode(‘utf-8’)
1 |
|
import urllib.request
request = urllib.request.Request(‘http://python.org')
response = urllib.request.urlopen(request)
print(response.read().decode(‘utf-8’))
1 |
|
from urllib import request,parse
url = ‘http://httpbin.org/post'
dict = {
‘name’:’Germey’
}
data=bytes(parse.urlencode(dict),encoding=”utf8”)
req=request.Request(url=url,data=data,method=’POST’)
response=request.urlopen(req)
print(response.read().encode(‘utf-8’))
1 |
|
Handler
(代理,通过连接到代理服务器,用来隐藏自己的ip)
cookie
:网站用来辨识用户身份存储在本地终端上的数据
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!