原生携带cookie访问网页

import urllib.request


"""
    直接获取  个人中心页面
    手动黏贴 复制到 PC 抓包的cookie
    放在请求头里面
"""

url = 'https://www.douban.com/'
# 请求头
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
    'Cookie':'douban-fav-remind=1; ; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1566639090%2C%22https%3A%2F%2Fwww.b......................*'
}
# 构建请求对象
request = urllib.request.Request(url,headers=headers)
# 发送请求对象  获取响应对象
response = urllib.request.urlopen(request)

data = response.read().decode('utf-8');
# data = str(response.read())
print(type(data))

# 保存到文件
with open('01cookie.html', 'w', encoding='utf-8') as f:
    f.write(data)


你可能感兴趣的:(分布式爬虫)