您的位置:首页 > 编程语言 > Python开发

Python2 爬虫(四) -- 模拟登陆(人人网和知乎)

2016-10-22 14:23 731 查看
人人网登录成功

#! /usr/bin/env python
# coding:utf-8

import sys
import re
import urllib2
import urllib
import requests
import cookielib

## 这段代码是用于解决中文报错的问题
reload(sys)
sys.setdefaultencoding("utf8")
#####################################################
# 登录人人
loginurl = 'http://www.renren.com/PLogin.do'
logindomain = 'renren.com'

class Login(object):
def __init__(self):
self.name = ''
self.passwprd = ''
self.domain = ''

self.cj = cookielib.LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)

def setLoginInfo(self, username, password, domain):
'''设置用户登录信息'''
self.name = username
self.pwd = password
self.domain = domain

def login(self):
'''登录网站'''
loginparams = {'domain': self.domain, 'email': self.name, 'password': self.pwd}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36'}
req = urllib2.Request(loginurl, urllib.urlencode(loginparams), headers=headers)
response = urllib2.urlopen(req)
self.operate = self.opener.open(req)
thePage = response.read()
print thePage

if __name__ == '__main__':
userlogin = Login()
username = 'your username'
password = 'your password'
domain = logindomain
userlogin.setLoginInfo(username, password, domain)
userlogin.login()


打印出来能看到自己的id肯定就是成功了。

# encoding=utf8
import cookielib
import urllib2
import urllib
from subprocess import Popen

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [
('User-agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0')]

def login():
username = ''
password = ''
cap_url = 'https://www.zhihu.com/captcha.gif?r=1466595391805&type=login'
cap_content = urllib2.urlopen(cap_url).read()

with open('code.gif', 'wb') as f:
f.write(cap_content)
Popen('code.gif', shell=True)

#写文件的方式获取验证码
#cap_file = open('/cap.gif', 'wb')
#cap_file.write(cap_content)
#cap_file.close()
captcha = raw_input('capture:')
url1 = 'http://www.zhihu.com/settings/profile'
url = 'https://www.zhihu.com/login/phone_num'
data = urllib.urlencode({"phone_num": "********", "password": "********", "captcha": captcha})
mydata = urllib2.urlopen(url, data).read()
print mydata

if __name__ == "__main__":
login()


验证码是用浏览器显示出来然后手动输入,但是我登录进去之后还是啥也没干。仅仅显示登录成功。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息