testPost.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. '''
  2. Created on 2017年5月16日
  3. @vsersion:python3.6
  4. @author: liuyuqi
  5. '''
  6. ##登录
  7. # -*- coding:utf-8 -*-
  8. import requests
  9. s = requests.Session()
  10. url1 = 'http://www.exanple.com/login' # 登陆地址
  11. url2 = "http://www.example.com/main" # 需要登陆才能访问的页面地址
  12. data = {"user": "user", "password": "pass"}
  13. headers = {"Accept": "text/html,application/xhtml+xml,application/xml;",
  14. "Accept-Encoding": "gzip",
  15. "Accept-Language": "zh-CN,zh;q=0.8",
  16. "Referer": "http://www.example.com/",
  17. "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
  18. }
  19. res1 = s.post(url1, data=data) # 登录
  20. res2 = s.post(url2) # 抓取
  21. res3 = requests.get(url2, cookies=res1.cookies, headers=headers)
  22. print(res2.content) # 获得二进制响应内容
  23. print(res2.raw) # 获得原始响应内容,需要stream=True
  24. print(res2.raw.read(50))
  25. print(type(res2.text)) # 返回解码成unicode的内容
  26. print(res2.url)
  27. print(res2.history) # 追踪重定向
  28. print(res2.cookies)
  29. print(res2.cookies['example_cookie_name'])
  30. print(res2.headers)
  31. print(res2.headers['Content-Type'])
  32. print(res2.headers.get('content-type'))
  33. print(res2.json) # 讲返回内容编码为json
  34. print(res2.encoding) # 返回内容编码
  35. print(res2.status_code) # 返回http状态码
  36. print(res2.raise_for_status()) # 返回错误状态码