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 · 2943 次点击
    这是一个创建于 1180 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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,简单又快捷
    Jwyt
        9
    Jwyt  
       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 代码原样复制下来执行是没问题的,确实是其它原因导致网络请求中断。
    Jwyt
        14
    Jwyt  
       2021-08-09 13:29:32 +08:00
    你自己的网络问题吧,我试了下正常的啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5909 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:26 · PVG 10:26 · LAX 19:26 · JFK 22:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.