38

I'm trying to find best way to capture links listed under response headers, exactly like this one and I'm using python requests module. Below is link which has Link Headers section on Python Requests page: docs.python-requests.org/en/latest/user/advanced/

But, in my case my response headers contains links like below:

{'content-length': '12276', 'via': '1.1 varnish-v4', 'links': '<http://justblahblahblah.com/link8.html>;rel="last">,<http://justblahblahblah.com/link2.html>;rel="next">', 'vary': 'Accept-Encoding, Origin'}

Please notice > after "last" which is not the case under Requests examples and I just cant seem to figure out how to solve this.

7
  • 1
    What is your question?
    – Konstantin
    Aug 31, 2015 at 13:46
  • Apologize if I wasn't clear, but I want to know how I can get Next and last link from response using rel in header response.
    – Malhar
    Aug 31, 2015 at 15:15
  • If requests do not work with this link header then you can parse it manually: it is not that hard.
    – Konstantin
    Aug 31, 2015 at 15:19
  • Use request's parsing function as a reference. See github.com/kennethreitz/requests/blob/…
    – Konstantin
    Aug 31, 2015 at 15:22
  • actually Im already doing that now, using re and looking for pattern. First, I'm parsing links exact links and then looking for page value(above links are just examples). But I guess I should rather look for pattern with link and rel
    – Malhar
    Aug 31, 2015 at 15:23

2 Answers 2

105

There is already a way provided by requests to access links header

response.links

It returns the dictionary of links header value which can easily parsed further using

response.links['next']['url']

to get the required values.

3
  • You can also check for a link by using if 'url' in response.links.get('next'): first.
    – cjbarth
    May 23, 2019 at 13:04
  • 1
    @cjbarth - if response.links.get('next'): worked better for me as it did not exist.
    – Dengar
    Mar 9, 2020 at 4:36
  • 1
    @westondeboer it can not be the answer since it doesn't solve OP's problem :-) Notice that OP asked about a very specific case that is not covered by requests builtin feature
    – Konstantin
    May 23, 2020 at 14:49
16

You can parse the header's value manually. To make things easier you might want to use request's parsing function parse_header_links as a reference.

Or you can do some find/replace and use original parse_header_links

In [1]: import requests

In [2]: d = {'content-length': '12276', 'via': '1.1 varnish-v4', 'links': '<http://justblahblahblah.com/link8.html>;rel="last">,<http://justblahblahblah.com/link2.html>;rel="next">', 'vary': 'Accept-Encoding, Origin'}

In [3]: requests.utils.parse_header_links(d['links'].rstrip('>').replace('>,<', ',<'))
Out[3]:
[{'rel': 'last', 'url': 'http://justblahblahblah.com/link8.html'},
 {'rel': 'next', 'url': 'http://justblahblahblah.com/link2.html'}]

If there might be a space or two between >, and < then you need to do replace with a regular expression.

2
  • this is perfect for my case.
    – Malhar
    Aug 31, 2015 at 16:26
  • 3
    Next answer, using response.links is the right thing to use.
    – Joac
    Apr 20, 2021 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.