爬虫模拟github登录

模拟登录是一件比较简单的事情,但是如果目标网站做了一些反爬措施,那么这个过程就不那么简单了。

如何模拟登陆github

在写爬虫代码之前一定要先分析http的请求流程,并且要成功登陆,观察需要带一些什么参数。

1.先分析 http 的请求流程
    - 请求url:
        Request URL: https://github.com/session

    - 请求方式:
        Request Method: POST

    - 请求头:
        - Referer: https://github.com/login
        - User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36

     #登陆后观察network里的请求发现需要带这么多东西
    - 请求体: (form data):
        commit: Sign in
        utf8: ✓
        authenticity_token: finQLn5TxHAabDmefQ2EbBXV27jDGlWm6DUu+u5u4J6QnPXBmkc76/QlDpx61v1NFf3AP8r+vg1Cq31G9Wxenw==
        ga_id:
        login: xxxxxxxx@qq.com
        password: Ai7071728109
        webauthn-support: supported
        webauthn-iuvpaa-support: supported
        required_field_a359:
        timestamp: 1577696492100  # 时间戳
        timestamp_secret: 03e50e82485174cadc2dda90916b93bfeadef0ac92643cbfde40e6c7f598bbb6


    - 1) 先往https://github.com/login页面发送get请求,获取authenticity_token与timestamp_secret随机加密字符串
    - 2) 携带加密字符串与请求体所有的信息,一并通过post请求访问https://github.com/session


2.再写爬虫代码

发现有两个我们没有自带的参数,一个是authenticity_token,另一个是timestamp_secret,所以就想办法找到这两个。

有些时候他们会把一些重要的数据放在element里面,所以之间在element里面查找,果然找到了这两个,于是就带上这两个参数,进行模拟登陆。

登录的时候发现,并没有成功,没有登录成功的原因就是,即使我们把所有请求头的参数都带上了,还是缺少了一个,这就是github的另外一个反爬,在你访问www:github.com/login的时候,就会给你返回一个login界面的cookie,如果要访问session(登录),就一定要带上,果不其然,在请求数据中带上了这个cookie之后,登录就成功了。登录成功后就生成了识别用户的cookie,访问个人主页就要带着这个cookie就行了。

代码

import requests
import re

# 1) 先往https://github.com/login页面发送get请求,获取authenticity_token与timestamp_secret随机加密字符串
url = 'https://github.com/login'
login_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}

login_response = requests.get(url, headers=login_headers)

# print(login_response.status_code)
# print(type(login_response.text))

# 2)先解析获取authenticity_token与timestamp_secret, 通过re模块实现
authenticity_token = re.findall('<input type="hidden" name="authenticity_token" value="(.*?)" />',
                                login_response.text,
                                re.S)[0]

timestamp_secret = re.findall('<input type="hidden" name="timestamp_secret" value="(.*?)" class="form-control" />',
                              login_response.text,
                              re.S)[0]

# print(authenticity_token)
# print(timestamp_secret)


# 3) 携带加密字符串与请求体所有的信息,一并通过post请求访问https://github.com/session

form_data = {
    'commit': 'Sign in',
    'utf8': '✓',
    'authenticity_token': authenticity_token,
    'ga_id': '',
    'login': 'xxxxxxxxx@qq.com',
    'password': 'xxxxxxxxx',
    'webauthn-support': 'supported',
    'webauthn-iuvpaa-support': 'supported',
    'required_field_a359': '',
    'timestamp': 1577696892274,  # 时间戳
    'timestamp_secret': timestamp_secret
}

session_url = 'https://github.com/session'
session_response = requests.post(
    session_url,
    data=form_data,
    cookies=login_response.cookies
)

print(session_response.status_code)
print(session_response.cookies)

# 4) 登录后,直接访问github主页, 前提是需要携带登录成功后的用户cookies值
# 携带cookies值的两种方式:
# - headers: 携带cookies值
# - cookies: 可以添加cookies值
# index_response = requests.get('https://github.com/', cookies=session_response.cookies)
# print(index_response.text)
# with open('github.html', 'w', encoding='utf-8') as f:
#     f.write(index_response.text)


# 5) 验证是否登录成功,校验邮箱
emails_response = requests.get('https://github.com/settings/emails', cookies=session_response.cookies)
print('xxxxxxxx@qq.com' in emails_response.text)

posted @ 2019-12-31 19:16  chanyuli  阅读(645)  评论(0编辑  收藏  举报