常见 http_status_code
- 200 请求被成功处理
- 301/302 永久重定向/临时重定向
- 403 没有权限访问
- 404 没有相对应的资源
- 500 服务器错误
- 503 服务器停机/正在维护
实际上这些 Status_code 一般是由服务器指定的,某些情况下是框架,或自动定义的
代码示例:
# -*- coding: utf-8 -*-
__author__ = 'Well'
import requests
try:
# py2 中是cookielib
import cookielib
except:
import http.cookiejar as cookielib
import re
# 实例化 session
session = requests.sessions()
# 实例化 LWPCookieJar ,以保存 cookies
session.cookies = cookielib.LWPCookieJar()
try:
session.cookies.load(ignore_discard=True)
except:
print('cookie 未能加载')
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0'
header = {
'Host': 'www.zhihu.com',
'Referer': 'https://www.zhihu.com/signin?next=%2F',
'User-Agent': user_agent
}
def is_login():
# 判断登录状态
rep = session.get('https://www.zhihu.com/settings/account', headers=header, allow_redirects=False)
if rep.status_code != 200:
return False
else:
return True
def get_xxrf():
# 获取xsrf code
response = session.get('www.zhihu.com', headers=header).text
return re.match('', response).group(1)
def get_index():
# 保存HTML
response = session.get('www.zhihu.com', headers=header).text
with open('index_page.html', 'wb') as fp:
fp.write(response.encode('utf-8'))
print('ok')
def zhihu_login(account, password):
# 知乎登录
if re.match(r'^1\d{10}', account):
post_url = 'https://www.zhihu.com/login/phone'
post_data = {
'xsrf': get_xxrf(),
'phone_num': account,
'password': password
}
elif '@' in account:
post_url = 'https://www.zhihu.com/login/email'
post_data = {
'xsrf': get_xxrf(),
'phone_num': account,
'password': password
}
else:
print('账号有误')
rep = session.post(url=post_url, data=post_data, headers=header).text
session.cookies.save()