python的cookie的操作
1.直接使用cookie的数据2.使用账号密码的形式
1.直接使用cookie的数据
from urllib
import request
url
= 'https://www.zhihu.com/hot'
head
= {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'cookie': '_z782c29c9af57f76542b63b260eb78bbf93f57ab15968"; unlock_ticket="AGACH7oaKAwmAAAAYAJVTWypT19nITTPCkxr19LIWA9iDNbilXxRpQ=="'
}
req
= request
.Request
(url
=url
, headers
=head
)
resp
= request
.urlopen
(req
)
print(resp
.read
().decode
('utf-8'))
2.使用账号密码的形式
from http
.cookiejar
import CookieJar
cookie
= CookieJar
()
handler
= request
.HTTPCookieProcessor
(cookie
)
opener
= request
.build_opener
(handler
)
from urllib
import parse
post_url
= ''
post_data
= parse
.urlencode
({
'username': '',
'password': ''
})
req
= request
.Request
(url
=post_url
, data
=post_data
.encode
('utf-8'))
opener
.open(req
)
url
= ''
rq
= request
.urlopen
(url
, handlers
=head
)
resp
= opener
.open(rq
)
print(resp
.read
().decode
('utf-8'))
转载请注明原文地址:https://tech.qufami.com/read-2382.html