怎样用Python设计一个爬虫模拟登陆知乎
1个回答
展开全部
#!/usr/bin/env python3# -*- coding: utf-8 -*-'''Required- requests (必须)- pillow (可选)Info- author : "xchaoinfo"- email : "xchaoinfo@qq.com"- date : "2016.2.4"Update- name : "wangmengcn"- email : "eclipse_sv@163.com"- date : "2016.4.21"'''import requeststry:
import cookielibexcept:
import http.cookiejar as cookielibimport reimport timeimport os.pathtry:
from PIL import Imageexcept:
pass# 构造 Request headersagent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'headers = {
'User-Agent': agent}# 使用登录cookie信息session = requests.session()session.cookies = cookielib.LWPCookieJar(filename='cookies')try:
session.cookies.load(ignore_discard=True)except:
print("Cookie 未能加载")def get_xsrf():
'''_xsrf 是一个动态变化的参数'''
index_url = 'httpm'
# 获取登录时需要用到的_xsrf
index_page = session.get(index_url, headers=headers)
html = index_page.text
pattern = r'name="_xsrf" value="(.*?)"'
# 这里的_xsrf 返回的是一个list
_xsrf = re.findall(pattern, html)
return _xsrf[0]# 获取验证码def get_captcha():
t = str(int(time.time()*1000))
captcha_url = 'htgif?r=' + t + "&type=login"
r = session.get(captcha_url, headers=headers)
with open('captcha.jpg', 'wb') as f:
f.write(r.content)
f.close()
# 用pillow 的 Image 显示验证码
# 如果没有安装 pillow 到源代码所在的目录去找到验证码然后手动输入
try:
im = Image.open('captcha.jpg')
im.show()
im.close()
except:
print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))
captcha = input("please input the captcha\n>")
return captchadef isLogin():
# 通过查看用户个人信息来判断是否已经登录
url = "hings/profile"
login_code = session.get(url,allow_redirects=False).status_code
if int(x=login_code) == 200:
return True
else:
return Falsedef login(secret, account):
# 通过输入的用户名判断是否是手机号
if re.match(r"^1\d{10}$", account):
print("手机号登录 \n")
post_url = 'hthone_num'
postdata = {
'_xsrf': get_xsrf(),
'password': secret,
'remember_me': 'true',
'phone_num': account,
}
else:
print("邮箱登录 \n")
post_url = 'htt
'_xsrf': get_xsrf(),
'password': secret,
'remember_me': 'true',
'email': account,
}
try:
# 不需要验证码直接登录成功
login_page = session.post(post_url, data=postdata, headers=headers)
login_code = login_page.text
print(login_page.status)
print(login_code)
except:
# 需要输入验证码后才能登录成功
postdata["captcha"] = get_captcha()
login_page = session.post(post_url, data=postdata, headers=headers)
login_code = eval(login_page.text)
print(login_code['msg'])
session.cookies.save()try:
input = raw_inputexcept:
passif __name__ == '__main__':
if isLogin():
print('您已经登录')
else:
account = input('请输入你的用户名\n> ')
secret = input("请输入你的密码\n> ")
login(secret, account)
import cookielibexcept:
import http.cookiejar as cookielibimport reimport timeimport os.pathtry:
from PIL import Imageexcept:
pass# 构造 Request headersagent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'headers = {
'User-Agent': agent}# 使用登录cookie信息session = requests.session()session.cookies = cookielib.LWPCookieJar(filename='cookies')try:
session.cookies.load(ignore_discard=True)except:
print("Cookie 未能加载")def get_xsrf():
'''_xsrf 是一个动态变化的参数'''
index_url = 'httpm'
# 获取登录时需要用到的_xsrf
index_page = session.get(index_url, headers=headers)
html = index_page.text
pattern = r'name="_xsrf" value="(.*?)"'
# 这里的_xsrf 返回的是一个list
_xsrf = re.findall(pattern, html)
return _xsrf[0]# 获取验证码def get_captcha():
t = str(int(time.time()*1000))
captcha_url = 'htgif?r=' + t + "&type=login"
r = session.get(captcha_url, headers=headers)
with open('captcha.jpg', 'wb') as f:
f.write(r.content)
f.close()
# 用pillow 的 Image 显示验证码
# 如果没有安装 pillow 到源代码所在的目录去找到验证码然后手动输入
try:
im = Image.open('captcha.jpg')
im.show()
im.close()
except:
print(u'请到 %s 目录找到captcha.jpg 手动输入' % os.path.abspath('captcha.jpg'))
captcha = input("please input the captcha\n>")
return captchadef isLogin():
# 通过查看用户个人信息来判断是否已经登录
url = "hings/profile"
login_code = session.get(url,allow_redirects=False).status_code
if int(x=login_code) == 200:
return True
else:
return Falsedef login(secret, account):
# 通过输入的用户名判断是否是手机号
if re.match(r"^1\d{10}$", account):
print("手机号登录 \n")
post_url = 'hthone_num'
postdata = {
'_xsrf': get_xsrf(),
'password': secret,
'remember_me': 'true',
'phone_num': account,
}
else:
print("邮箱登录 \n")
post_url = 'htt
'_xsrf': get_xsrf(),
'password': secret,
'remember_me': 'true',
'email': account,
}
try:
# 不需要验证码直接登录成功
login_page = session.post(post_url, data=postdata, headers=headers)
login_code = login_page.text
print(login_page.status)
print(login_code)
except:
# 需要输入验证码后才能登录成功
postdata["captcha"] = get_captcha()
login_page = session.post(post_url, data=postdata, headers=headers)
login_code = eval(login_page.text)
print(login_code['msg'])
session.cookies.save()try:
input = raw_inputexcept:
passif __name__ == '__main__':
if isLogin():
print('您已经登录')
else:
account = input('请输入你的用户名\n> ')
secret = input("请输入你的密码\n> ")
login(secret, account)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询