http-useragent.rst 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. User-Agent and Referer Headers
  2. ==============================
  3. :program:`Universal Feed Parser` sends a default User-Agent string when it
  4. requests a feed from a web server.
  5. The default User-Agent string looks like this:
  6. ::
  7. UniversalFeedParser/5.0.1 +http://feedparser.org/
  8. If you are embedding :program:`Universal Feed Parser` in a larger application,
  9. you should change the User-Agent to your application name and
  10. :abbr:`URL (Uniform Resource Locator)`.
  11. Customizing the User-Agent
  12. --------------------------
  13. ::
  14. >>> import feedparser
  15. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml',
  16. agent='MyApp/1.0 +http://example.com/')
  17. You can also set the User-Agent once, globally, and then call the ``parse``
  18. function normally.
  19. Customizing the User-Agent permanently
  20. --------------------------------------
  21. ::
  22. >>> import feedparser
  23. >>> feedparser.USER_AGENT = "MyApp/1.0 +http://example.com/"
  24. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml')
  25. :program:`Universal Feed Parser` also lets you set the referrer when you
  26. download a feed from a web server. This is discouraged, because it is a
  27. violation of `RFC 2616 <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.36>`_.
  28. The default behavior is to send a blank referrer, and you should never need to
  29. override this.
  30. Customizing the referrer
  31. ------------------------
  32. ::
  33. >>> import feedparser
  34. >>> d = feedparser.parse('http://feedparser.org/docs/examples/atom10.xml',
  35. referrer='http://example.com/')