Python网络爬虫之模拟登陆
2018-03-16 10:44
666 查看
为什么要模拟登陆
Python网络爬虫应用十分广泛,但是有些网页需要用户登陆后才能获取到信息,所以我们的爬虫需要模拟用户的登陆行为,在登陆以后保存登陆信息,以便浏览该页面下的其他页面。保存用户信息
模拟登陆后有两种方法可以保存用户信息,通过Session来保存登陆信息或者通过Cookie来保存登陆信息一、Session的用法
# 导入requests模块 import requests # 通过requests的Session来请求网页 s = requests.Session() r = s.post(url, headers=headers)
二、Cookie的用法
import urllib.request, http.cookiejar # 初始化Cookie cookie = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie)) # 把opener配置为全局 当然也可以不配置全局通过opener来请求网页 urllib.request.install_opener(opener)
模拟登陆实践
我们以豆瓣网为例模拟用户登陆,然后爬取登陆后的用户界面(1)找到请求表单登陆一般是通过Post请求来实现的,其传递参数为一个表单,如果要成功登陆,我们需要查看该表单传递了哪些内容,然后构造表单做Post请求。怎么获取表单了,我们只需要打开浏览器右键查看,然后输入账号密码,点击登陆查看其NetWork中的请求,找到表单信息即可(推荐使用谷歌浏览器),该信息中还能找到请求的url。
表单信息
URL
(2)构建表单表单的key值我们可以通过右键页面检查页面源代码,在页面源码中获得静态的值(还有些动态信息需要手动获取)formdata = {
'redir': 'https://www.douban.com',
'form_email': '账号',
'form_password': '密码',
'login': u'登陆'
}
(3)伪装成浏览器进行登录我们只需要给请求添加上Headers即可headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/55.0.2883.87 Safari/537.36'}
(4)获取验证码第二步的表单其实还不完整,还差两条跟验证码有关的信息,这两条信息是动态变化的,所以我们要手动获取r = s.post(url_login, headers=headers)
content = r.text
soup = BeautifulSoup(content, 'html.parser')
captcha = soup.find('img', id='captcha_image')#当登陆需要验证码的时候
if captcha:
captcha_url = captcha['src']
re_captcha_id = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
captcha_id = re.findall(re_captcha_id, content)
print(captcha_id)
print(captcha_url) # 打印验证码url
captcha_text = input('Please input the captcha:') # 手动输入验证码
formdata['captcha-solution'] = captcha_text # 添加表单信息
formdata['captcha-id'] = captcha_id
(5)登录
r = s.post(url_login, data=formdata, headers=headers) # 将表单信息传入参数中请求页面即可登录
完整代码
# -*- coding: utf-8 -*-import requests
import re
from bs4 import BeautifulSoup
s = requests.Session()
url_login = 'https://accounts.douban.com/login'
formdata = {
'redir': 'https://www.douban.com',
'form_email': '账号',
'form_password': '密码',
'login': u'登陆'
}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/55.0.2883.87 Safari/537.36'}
r = s.post(url_login, data=formdata, headers=headers)
content = r.text
soup = BeautifulSoup(content, 'html.parser')
captcha = soup.find('img', id='captcha_image')#当登陆需要验证码的时候
if captcha:
captcha_url = captcha['src']
re_captcha_id = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
captcha_id = re.findall(re_captcha_id, content)
print(captcha_id)
print(captcha_url)
captcha_text = input('Please input the captcha:')
formdata['captcha-solution'] = captcha_text
formdata['captcha-id'] = captcha_id
r = s.post(url_login, data=formdata, headers=headers)
with open('contacts.html', 'w+', encoding='utf-8') as f:
f.write(r.text)
运行结果
登陆成功相关文章推荐
- Python3网络爬虫:使用Cookie-模拟登陆
- Python 爬虫模拟登陆知乎
- Python网络爬虫之模拟登录(以知乎为例)
- Python 网络爬虫--关于简单的模拟登录实例讲解
- Python爬虫学习(8):浙大软院网络登陆保持
- python 爬虫 教务系统模拟登陆 并下载课表
- 人生苦短,我用Python--爬虫模拟登陆教务处并且保存数据到本地
- Python爬虫----爬虫入门(4)---urllib2 模拟登陆
- Python爬虫模拟登陆知乎
- Python 爬虫模拟登陆知乎
- 【Python爬虫】教务处模拟登陆
- Python——新浪微博爬虫之模拟登陆
- python-49: 爬虫模拟登陆源码
- python 爬虫模拟登陆
- 02精通Python网络爬虫——模拟浏览器&超时设置
- [置顶] 豆瓣网络爬虫-java网络爬虫[验证码模拟登陆]详细介绍
- 【网络爬虫】【python】网络爬虫(三):模拟登录——伪装浏览器登录爬取过程
- python3版本爬虫系列之模拟登陆CSDN
- Python爬虫模拟登陆知乎
- Python爬虫——模拟登陆爬取知乎页面