彦祖们,这个响应头的 location 怎么获取啊? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mingtdlb
V2EX    Python

彦祖们,这个响应头的 location 怎么获取啊?

  •  
  •   mingtdlb 2021-08-08 15:14:12 +08:00 3441 次点击
    这是一个创建于 1575 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT,怎么获取 location 呀?求指教

    login

    [root@centos ~]# cat t.py import requests headers = { "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62" } s = requests.session() r = s.get('http://jd.com/', headers=headers, allow_redirects=False) print (r.status_code) r_url = r.headers["Location"] print (r_url) 

    执行报错了

    [root@centos ~]# python3 t.py Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen chunked=chunked, File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "<string>", line 3, in raise_from File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request httplib_respOnse= conn.getresponse() File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse response.begin() File "/usr/lib64/python3.6/http/client.py", line 307, in begin version, status, reason = self._read_status() File "/usr/lib64/python3.6/http/client.py", line 268, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib64/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) ConnectionResetError: [Errno 104] Connection reset by peer During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2] File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 532, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 769, in reraise raise value.with_traceback(tb) File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen chunked=chunked, File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "<string>", line 3, in raise_from File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request httplib_respOnse= conn.getresponse() File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse response.begin() File "/usr/lib64/python3.6/http/client.py", line 307, in begin version, status, reason = self._read_status() File "/usr/lib64/python3.6/http/client.py", line 268, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib64/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "t.py", line 8, in <module> allow_redirects=False) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 555, in get return self.request('GET', url, **kwargs) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) [root@centos ~]# 
    tony9413
        1
    tony9413  
       2021-08-08 15:26:13 +08:00
    日志上看 get 时候就报错了,程序走不到下面
    Ptu2sha
        2
    Ptu2sha  
       2021-08-08 17:37:01 +08:00
    ..百度一下错误原因就知道连接被重置了 和你代码获取无关
    mingtdlb
        3
    mingtdlb  
    OP
       2021-08-08 20:46:43 +08:00
    @Ptu2sha ?上面写了啊,被对方重置了。我的意思是要怎么解决
    Cusmate
        4
    Cusmate  
       2021-08-08 23:30:10 +08:00
    代码获取方式没问题,你要解决的是什么问题?
    initd
        5
    initd  
       2021-08-08 23:31:41 +08:00
    s = sessions.Session()

    更好的写法是
    r = requests.get

    我们可能不是同一个版本的 requests,所以多看文档
    toaruScar
        6
    toaruScar  
       2021-08-08 23:46:53 +08:00
    截图里面请求的不是 22.22.22.22 ,而是 jd.com
    你可以试试看在浏览器里请求 jd.com ,成功之后把浏览器请求时用了的 headers 全抄过去
    mingtdlb
        7
    mingtdlb  
    OP
       2021-08-08 23:48:25 +08:00
    @Cusmate 想要拿到响应头 location 的值,目的是拿到 location 值中的 token 值
    Cusmate
        8
    Cusmate  
       2021-08-08 23:53:33 +08:00
    @mingtdlb 建议直接用 curl,简单又快捷
    Dockerfile
        9
    Dockerfile  
       2021-08-09 08:11:32 +08:00
    @initd 更好的写法?。。你不会是不知道 session 啥意思吧
    muzuiget
        10
    muzuiget  
       2021-08-09 10:18:08 +08:00
    链接被重置,本来就连不上,换个 url 吧。
    mingtdlb
        11
    mingtdlb  
    OP
       2021-08-09 11:25:28 +08:00
    @muzuiget
    @toaruScar
    这个认证,在浏览器中,就是随便使用一个 url,只要不是 https 的都行,然后他会自动跳转到认证页面。认证服务器并没有给当独的认证地址。
    mingtdlb
        12
    mingtdlb  
    OP
       2021-08-09 11:49:15 +08:00
    @Cusmate 开始使用 curl 发 HEAD 请求的,但是也是直接给我重置了...
    Cusmate
        13
    Cusmate  
       2021-08-09 12:10:36 +08:00
    不清楚了,我把你的 python 代码原样复制下来执行是没问题的,确实是其它原因导致网络请求中断。
    Dockerfile
        14
    Dockerfile  
       2021-08-09 13:29:32 +08:00
    你自己的网络问题吧,我试了下正常的啊
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     783 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 20:33 PVG 04:33 LAX 12:33 JFK 15:33
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86