Python爬虫——使用Cookie

Python爬虫——使用Cookie

本文使用的版本为Python3,使用的IDE为Pycharm

  • 示例代码如下:
# 使用Cookie Demo

# 导入模块
from urllib import request, parse, error
from http import cookiejar

# 此处为某网站登陆URL
url1 = ""
# 填充数据
postdata = parse.urlencode({
    "username":"",
    "password":""
}).encode("utf-8")
req = request.Request(url1, postdata)
# 伪装成浏览器
req.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36")
# 创建CookieJar
cjar = cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cjar))
request.install_opener(opener)
try:
    data = request.urlopen(req).read()
    fhandle = open("D:\\upackage1.html", "wb")
    fhandle.write(data)
    fhandle.close()
except error.HTTPError as e:
    print("url1: " + str(e.code))
    print("url1: " + str(e.reason))

# 此处为上述网站登陆后URL
url2 = ""
try:
    request.urlretrieve(url2, filename="D:\\upackage2.html")
except error.HTTPError as e:
    print("url2: " + str(e.code))
    print("url2: " + str(e.reason))

猜你喜欢

转载自blog.csdn.net/m0_37770300/article/details/81276112