小醉寒i

【原创】使用python保存网页为html格式
from urllib import request url = 'https://blog.iscrooge....
扫描右侧二维码阅读全文
29
2019/08

【原创】使用python保存网页为html格式

from urllib import request

url = 'https://blog.iscrooge.cn/admin'

#设置一个未登陆的cookie

# headers = {
#     'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
#     'cookie' : 'dfbcbf37f7ae95401c56146fd3ecad92latest_time_id=10'
# }


#设置一个登陆的cookie
headers = {
    'user-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
    'cookie' : 'dfbcbf37f7ae95401c56146fd3ecad92latest_time_id=10; dfbcbf37f7ae95401c56146fd3ecad92__typecho_uid=1; dfbcbf37f7ae95401c56146fd3ecad92__typecho_authCode=%24T%24QRcK9Z434d1ff6a5dbd6c963f1f8574b9328a8288; PHPSESSID=qtse3h3t3m65imul0fh47b1ofr'
}

response = request.Request(url,headers=headers)     #构建一个Request请求
result = request.urlopen(response)      #发送请求


# with open('blog.html','w') as fp:     #将未登陆的cookie的页面保存为blog.html格式储存到本地
#     fp.write(result.read().decode('utf-8'))

with open('blog_admin.html','w') as fp:     #将登陆的cookie的页面保存未blog_admin.html格式储存到本地
    fp.write(result.read().decode('utf-8'))

扫描二维码,在手机上阅读!
Last modification:August 29th, 2019 at 09:27 am
果觉得我的文章对你有用,请随意赞赏瓶饮料

Leave a Comment