2)实现github自动登陆获取信息
# -*- coding:utf-8 -*- # __author__ = 'lixiang' # 实现github自动登陆和获取数据 import requests from bs4 import BeautifulSoup #第一步获取github的CSRFtoken r1=requests.get(url="https://github.com/login") bs=BeautifulSoup(r1.text,features="html.parser") tag=bs.find(name="input",attrs={"name":"authenticity_token"}) csrftoken=tag.attrs.get("value") r1_cookies=r1.cookies.get_dict() #第二步登陆,要带第一次的cookie,给他授权 post_dict={ "commit":"Sign in", "utf8":"✓", "authenticity_token":csrftoken, "login":"xxx", "password":"xxx" } r2=requests.post( url="https://github.com/session", data=post_dict, cookies=r1_cookies, ) #登录成功获取cookie r2_cookies=r2.cookies.get_dict() print(r1_cookies) print(r2_cookies) """ {'logged_in': 'no', '_gh_sess': 'aFVOSWJqVTJwRjVxZmg0ZmxYNldCNCtPYVdseENaNlBCTzdXTG1ZZVUxNHdHbWNhZzA5ekRvdXhTWmpzTzJHVEVOTWh1R0dWenNkWFVrRlhST1J3TTZISER0eGFveHRleXlad0F2Z2xMQ1RDbldQN2UvRjNCelRadmNFNWs2Z1h2LzlJdmdMUU40UGpZTVBqWDNnRDJla1ErblNWTFlaSzFscUh0T0dvMThDL2kxeEdESUF5bTdlOXM3Q0E2Y1FrSzdGdjVScTVNaHBkU25jNEZiVm91V0dYaUQ4Q2lUTkxycjRWdlR6Q2NrTGJBLzR3L1NyTlViYmFEeEpuRVEvUGh1T0xScVV6S1c5OTRFVElkUlVDMDJXM1kwd2Y3d09IWjFBa0ZvWXJNNmxOQlJJcFN3VitFalZSZFRVMk9Cd00tLTdJeE01dFhuR01sYmdZVmUvK2FZOWc9PQ%3D%3D--74f6aa35aadf3a7f53d2a2c9c84196ea680284fb'} {'logged_in': 'yes', '__Host-user_session_same_site': 'p9mOvXkKsooBAWnzaWI4MTCEY2NcMfOQ6ZplGjIdE-buENW-', '_gh_sess': 'alU5c290U1Y2aXZPVEVFTjdUWEpWUnpMdVUrVjNsZmJCU2Vrdi9VL3R1N0hVK2w1MkFMUmdOaDRXdXYwditsRm1IOU5jbVlBQ1MzbHI3R2p3dGlhdnFuYWZUWENzN3FwK3QzZStpeHo1ZmttS3Nmb2Fmbm5DRWhCbWpNbjdNQU01T3NJYWs4ZVhUaks3aVNQSjdOZzhUUmNPTm5kT21qZ1dpMFFwd3JkTmVURjNUU0hnK2cyNXdkd1hTZVZxTjl0NzVoV1V3eDhSNGJtM3FRaDRxejNWTUZuQjkyay9KN2NSdDB2TU1tSm1WS3dmaHdXL3ZUS3VEQWVnVUJLRGRkc1Axa09VV1VpU3BSWnp3L3pYRGo5WDZoVGVwZXovbG9uVk5hZTBFbTBtM0VhRHJmYzZHMUxBM3JYWHJ2R1F2NW9oT3prdlpOYmZMbk1PYUpPbVpBQS91bWxSWHJzM1IrNVYwSzI1TzIwdUFtUjRWRWFmb3A0SEd4VlZRcE8yRmt5YThvc1FEcGJNeGNwRGdQZ3ZHeGJzTXlkY3MyN05zdjhVUDhzbmNGQWFPRlJBQ1RuZXQvRFYrU3JaVk5zVXhkaC0tQnZ1MXdGbzBWK0gzNjlxTzJFdUhkdz09--cdbc5200af5265fbfc5e9c45c9e3cf5a6872d542', 'user_session': 'p9mOvXkKsooBAWnzaWI4MTCEY2NcMfOQ6ZplGjIdE-buENW-'} """ #把r2_cookies更新到r1_cookies r1_cookies.update(r2_cookies) #获取仓库信息 r3=requests.get( url="https://github.com/settings/repositories", cookies=r1_cookies ) print(r3.text)