用requests,BeautifulSoup模块获取Github的个人信息

本文介绍了一种使用Python的requests和BeautifulSoup库实现GitHub自动化登录的方法,并演示了如何从登录后的页面中抓取用户的个人信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import requests
from bs4 import BeautifulSoup

Gtoken = requests.get(
    url="https://github.com/login"
    #有时候注意模拟浏览器的行为,加上请求头
)
print("打开登陆页面状态",Gtoken.status_code)
TokenSoup = BeautifulSoup(Gtoken.text,"html.parser")
TokenValue = TokenSoup.find(name="input",attrs={"name":"authenticity_token"})
Cookies_list = Gtoken.cookies.get_dict()

SignIn = requests.post(
    url="https://github.com/session",
    data={
        "commit":"Sign+in",
        "utf8":"✓",
        "authenticity_token":TokenValue.get("value"),
        "login":"61****371@qq.com",
        "password":"******"
    },
    cookies = Cookies_list
)
print("登陆账号状态",SignIn.status_code)
SignInSoup = BeautifulSoup(SignIn.text,"html.parser")
UserName = SignInSoup.find(name='strong',attrs={"class":"css-truncate-target"})
Cookies_Sigin = SignIn.cookies.get_dict()

Nameurl =  "https://github.com/{0}".format(UserName.text)
UserPro = requests.get(
    url = Nameurl,
    cookies = Cookies_Sigin
)
print("打开个人主页状态",UserPro.status_code)
UserNameSoup = BeautifulSoup(UserPro.text,"html.parser")
UserName_gh = UserNameSoup.find(name="span",attrs={"class":"p-name vcard-fullname d-block overflow-hidden"})
print("获取个人信息之名字:",UserName_gh.text)   #这里就简单地获取github中的名字
#print(SignInSoup)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值