使用一般爬蟲登錄Github獲取個人信息 ---- 第2章節

1. 獲取Github站點的token
def get_cookie_token():
    """
    獲取github的cookies和csrf token
    :param url:請求的地址
    :return:token,cookies_dict
    """
    url = 'https://github.com/login'
    resp = requests.get(url)
    login_cookie_dict = resp.cookies.get_dict()
    bs = BeautifulSoup(resp.text, features='html.parser')
    token = bs.find(name='input', attrs={'name': 'authenticity_token'}).get('value')  # 根據name獲取value
    return login_cookie_dict, token
2. 獲取Github站點的cookies

Form data:
在這裏插入圖片描述

def get_cookies_dict():
    login_cookies_dict, token = get_cookies_token()
    url = 'https://github.com/session'
    username = 'thanlonsmith'
    pwd = 'github'
    resp = requests.post(url=url, data={
        'commit': 'Sign in',
        'authenticity_token': token,
        'ga_id': '1152486977.1587620692',
        'login': username,
        'password': pwd,
        'webauthn - support': 'supported',
        'webauthn - iuvpaa - support': 'unsupported',
        'return_to': '',
        'required_field_6d18': '',
        'timestamp': '1588985314673',
        'timestamp_secret:': 'b9bc4e638fe1e58fc75abfb15ab3a90c30357a94a70e1b07b420e2b625897ee'
    }, cookies=login_cookies_dict)
    get_cookies = resp.cookies.get_dict()
    cookies_dict = {}
    cookies_dict.update(login_cookies_dict)
    return cookies_dict
3. 登錄Github獲取個人信息
def get_profile():
    cookies_dict = get_cookies_dict()
    url = 'https://github.com/settings/profile'
    resp = requests.get(url, cookies_dict)
    print(resp.text)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章