0
私はPythonリクエストライブラリを使用してサイトにログインしています。この目的のために、私はログインページのいくつかのデータを取得し、それと同じページにPOST要求を行う必要があります。しかし、私はcustom authenticationを作るためにBaseAuth
クラスを提供要求に気づいPythonはBaseAuthクラスとカスタムPOSTデータを要求しますか?
import requests
from bs4 import BeautifulSoup
from contextlib import contextmanager
def get_data(soup, ids, **kwargs):
pass
@contextmanager
def login_session(username='user', password='pass'):
auth = {'username': username, 'password': password}
url = 'http://example.com/Login.aspx'
ids = ('#this', '#other')
with requests.Session() as s:
soup = BeautifulSoup(s.get(url).content)
s.post(url, data=get_data(soup, ids, **auth))
yield s
with login_session() as s:
page = s.get('http://example/protected.aspx').content
:
私の最初のアプローチは、のようなものでした。
だから、私は自分のCustomAuth
import requests
class CustomAuth(AuthBase):
ids = ('#this', '#other')
url = "http://example.com/Login.aspx"
def __init__(self, username, password):
self.username = username
self.password = password
def _get_data(self, soup):
pass
def __call__(self, r):
'''Prepare data for POST request'''
with requests.Session() as s:
soup = BeautifulSoup(s.get(self.url).content)
r.data = self._get_data(soup)
return r
with requests.Session() as s:
s.post(CustomAuth.url, auth=CustomAuth('user','pass'))
を実装しようとしたが、期待どおりに動作しません。 この場合、BaseAuth
を使用できますか?