当前位置:Gxlcms > Python > Python中使用urllib2防止302跳转的代码例子

Python中使用urllib2防止302跳转的代码例子

时间:2021-07-01 10:21:17 帮助过:106人阅读

说明:python的urllib2获取网页(urlopen)会自动重定向(301,302)。但是,有时候我们需要获取302,301页面的状态信息。就必须获取到转向前的调试信息。

下面代码将可以做到避免302重定向到新的网页

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #encoding=utf-8
  4. #Filename:states_code.py
  5. import urllib2
  6. class RedirctHandler(urllib2.HTTPRedirectHandler):
  7. """docstring for RedirctHandler"""
  8. def http_error_301(self, req, fp, code, msg, headers):
  9. pass
  10. def http_error_302(self, req, fp, code, msg, headers):
  11. pass
  12. def getUnRedirectUrl(url,timeout=10):
  13. req = urllib2.Request(url)
  14. debug_handler = urllib2.HTTPHandler(debuglevel = 1)
  15. opener = urllib2.build_opener(debug_handler, RedirctHandler)
  16. html = None
  17. response = None
  18. try:
  19. response = opener.open(url,timeout=timeout)
  20. html = response.read()
  21. except urllib2.URLError as e:
  22. if hasattr(e, 'code'):
  23. error_info = e.code
  24. elif hasattr(e, 'reason'):
  25. error_info = e.reason
  26. finally:
  27. if response:
  28. response.close()
  29. if html:
  30. return html
  31. else:
  32. return error_info
  33. html = getUnRedirectUrl('http://jb51.net')
  34. print html

人气教程排行