使用python请求登录github

2024-06-09 01:16:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python请求登录github。在登录页面中,我发现我需要以下数据发布到服务器

       commit: Sign in
       authenticity_token:    3YV9FG87QiYCga1Ue3IGMRSh88uDWBBhVW2Plwd22uDe+CBPbK/n+I+TC0ozTBsvk7QEPjNdFLvCil/9mdMr5A==    ga_id: 642223273.1583769258
       login: XXX
       password: XXX
       webauthn-support: supported
       webauthn-iuvpaa-support: unsupported
       return_to: 
       required_field_57c4: 
       timestamp: 1583770320678
       timestamp_secret:    fdc0b523bcd384d633c714f78fc8fd5d909dde3a05c0501345f358af2890fdec

我可以使用chrome从登录页面获取“gau id”,但当我使用python请求获取login page.html时,我发现它的内容是空的

chrome正在获取以下数据:

meta name=“octalytics-dimension-ga_id”content=“740927323.1583770634” class="js-octo-ga-id"

当我使用python请求时,我得到了这样的数据,但是内容是空白的

meta name=“八分体-维度-ga_id”内容=” class="js-octo-ga-id"

import requests
import re

def login():

    headers = {
            'Referer': 'https://github.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
            'Host': 'github.com',
    }

    url1 = "https://github.com/login"
    response = session.get(url1, headers=headers).content
    print(response)

打印响应时,我发现“octalytics-dimension-ga_id”内容为空。为什么?


Tags: 数据githubcomid内容supportlogin页面
1条回答
网友
1楼 · 发布于 2024-06-09 01:16:21

如果您想知道如何使用您的代码登录,那么以下是正确的方法。但是请注意,GitHub已经提供了API,您可以将其用于登录

import requests
from bs4 import BeautifulSoup
import re


def Main(url):
    with requests.Session() as req:
        r = req.get(url)
        soup = BeautifulSoup(r.text, 'html.parser')
        token = soup.find("input", {'data-csrf': 'true'}).get("value")
        timest = soup.find("input", {'name': 'timestamp'}).get("value")
        timestsc = soup.find(
            "input", {'name': 'timestamp_secret'}).get("value")
        rq = soup.find(
            "input", {'type': 'text', 'name': re.compile("^required_field")}).get("name")
        data = {
            'commit': 'Sign+in',
            'authenticity_token': token,
            'login': 'test@test.com',
            'password': 'lalala',
            'webauthn-support': 'supported',
            'webauthn-iuvpaa-support': 'unsupported',
            'return_to': '',
            rq: '',
            'timestamp': timest,
            'timestamp_secret': timestsc
        }
        r = req.post(url, data=data)
        print(r.text)


Main("https://github.com/session")

Since we haven't included valid user/pass so if you checked the output. you will find the following which confirm we are on the correct way.

输出:

Incorrect username or password.

相关问题 更多 >