爬虫:模拟登录豆瓣

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yk_0311/article/details/83211428

模拟登录

一般的模拟登录:向登录的接口POST表单数据

但是有时候POST数据不容得到,可以附带Cookies值去发送请求,这样子的成功率高,简单易实现。

上面的例子模拟登录豆瓣有几个要点

1.确定提交的数据

​ 打开登录界面–>打开Network选项卡–>在登入界面输入账号密码登录—>查看选项卡下的信息

​ (找到Form Data所在的数据块)可看到需要提交的数据,然后在General中可以看到请求的URL

2.找到需要提交的数据

​ 如果登录时需要验证码,去登录页面找到图片的url,可以发现captcha-id字段的数据也在其中

提取出来就好,其他数据是容易得到的

3.需要建立会话维持

​ 如果不建立同一个会话,那么在你去请求获得页面信息和向页面POST信息时是打开了两个页面(虽然是相同的url),如果需要验证码验证,两个页面的验证码肯定是不同的。所以要维持会话,在一个页面下获得、POST信息。

4.验证码的处理

​ 将图片下载下来,然后再打开,在线自己识别。。。。。

代码:

# 向登录接口POST表单数据
import requests
from scrapy import Selector
import re
from PIL import Image


class Login(object):
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36'
        }
        self.login_url = 'https://accounts.douban.com/login'  # 登录界面的url
        self.session = requests.Session()  # 会话维持

    # 找到提交表单所需要的验证码图片与captcha_id
    def images(self):
        response = self.session.get(self.login_url, headers=self.headers)
        print(response)
        # 使用了Scraoy中的Selector,建立一个选择器对象,然后调用css()来提取信息
        selector = Selector(text=response.text)
        # 找到验证码图片的url
        images_url = selector.css('div img.captcha_image::attr(src)').extract_first()
        if images_url:  # 如果登录时需要验证码
            match = re.search(r'id=(\w+:en)', images_url)  # 使用正则表达式匹配出captcha-id
            if match:
                captcha_id = match.group(1)
                # print(captcha_id)
            # 将验证码图片存储起来,以便后续使用
            path = 'D://IDE//Pycharm//《网络爬虫实战开发》//模拟登录//' + captcha_id[:4] + '.jpg'
            r = requests.get(images_url)
            with open(path, 'wb') as f:
                f.write(r.content)
            # print(images_url)
        else:  # 没有找到验证码,就是登录时不需要验证码
            captcha_id = None
        return captcha_id

    def login(self):
        captcha_id = self.images()  # captcha—id
        # print(captcha_id)
        if captcha_id:  # 存在验证码
            image = Image.open('D://IDE//Pycharm//《网络爬虫实战开发》//模拟登录//' + captcha_id[:4] + '.jpg')
            image.show()  # 显示图片
            captcha_solution = input('请输入验证码:')
            FormData = {  # 表单数据
                'source': 'index_nav',
                'redir': 'https://www.douban.com/',
                'form_email': '账号',
                'form_password': '密码',
                'captcha-solution': captcha_solution,
                'captcha-id': captcha_id
            }
        else:  # 不存在验证码时提交的表单数据
            FormData = {
                'source': 'index_nav',
                'redir': 'https://www.douban.com/',
                'form_email': '账号',
                'form_password': '密码'
            }
        response = self.session.post(self.login_url, data=FormData, headers=self.headers)
        if response.status_code == 200:
            print('success')
        response = self.session.get('https://www.douban.com/explore/', headers=self.headers)  # 查看豆瓣页面下的其他信息
        if response.status_code == 200:
            print(response.text)


if __name__ == '__main__':
    login = Login()
    login.login()

github:https://github.com/YilK/Web-Crawler/blob/master/DEMO06-模拟登录豆瓣/模拟登录豆瓣.py

猜你喜欢

转载自blog.csdn.net/Yk_0311/article/details/83211428