0

Sometimes it's necessary to change the version attribute when retrieving a request using FancyURLopener, e.g.

from urllib.request import FancyURLopener

class NewOpener(FancyURLopener):
    version = 'Some fancy thing'

url = 'www.google.com'
opener = NewOpener.retrieve(url, 'google.html')

Is there an equivalence in the requests library when using requests.get()?

alvas
  • 115,346
  • 109
  • 446
  • 738
  • Um... I am... really... confused. Do you want to set `User-Agent` in `requests`? – Sraw Apr 03 '18 at 08:52
  • Is the `FancyURLopener.version` a `User-Agent`? It's just to set an attribute to the opener but honestly, I'm not exactly sure how `FancyURLopener` is treating the `version` attribute. Is it something special? – alvas Apr 03 '18 at 08:55
  • 1
    According to official doc, `FancyURLopener`'s `version` is inherited from `URLopener`. And `version` in `URLopener` is used to specify "the user agent of the opener object". ref: https://docs.python.org/3/library/urllib.request.html#urllib.request.URLopener.version – Sraw Apr 03 '18 at 08:58

1 Answers1

0

As @Sraw commented, the "version" is basically the user-agent file in the header, so

requests.get(url, headers={'User-agent': 'Some fancy thing'}
alvas
  • 115,346
  • 109
  • 446
  • 738