Python-requests: Where to find all possible attributes?
本问题已经有最佳答案,请猛点这里访问。
我将python请求与lambda函数结合使用(如果调用其他函数,请纠正我)以获取URL。考虑到url根据EDOCX1的值变化(0),我想打印它进行调试。
对我来说幸运的是,
1 2 3 4 5 | import time, requests epoch = lambda: int(time.time()*1000) kiwi = s.get('http://www.example.com/api?do=%i' % epoch()) print(kiwi.url) #I found the .url attribute by chance. ^ |
一般来说,您可以使用
1 2 3 4 5 6 7 8 | >>> import requests >>> response = requests.get('http://httpbin.org/get?foo=bar') >>> dir(response) ['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url'] >>> 'url' in dir(response) True >>> vars(response).keys() ['cookies', '_content', 'headers', 'url', 'status_code', '_content_consumed', 'encoding', 'request', 'connection', 'elapsed', 'raw', 'reason', 'history'] |
您也可以使用
对于
在外壳中使用
1 2 3 4 5 6 7 8 9 10 11 12 | >>> import requests >>> req = requests.get('http://www.google.com') >>> dir(req) ['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url'] |