Python 아날로그 브라우저 기반 http 요청 보내기

1.urllib2를 사용하여 실현

#! /usr/bin/env python
# -*- coding=utf-8 -*- 

import urllib2
url="https://www.baidu.com"
req_header = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
"Accept":"text/html;q=0.9,*/*;q=0.8",
"Accept-Charset":"ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"Accept-Encoding":"gzip",
"Connection":"close",
"Referer":None # , host
}
req_timeout = 5
req = urllib2.Request(url,None,req_header)
resp = urllib2.urlopen(req,None,req_timeout)
html = resp.read()
print(html)
2. requests 모듈 사용하기
(1).get 요청

#-*- coding:utf-8 -*-
import requests

url = "https://www.baidu.com"
payload = {"key1": "value1", "key2": "value2"}
r = requests.get(url, params=payload)
print r.text
(2).post 요청

#-*- coding:utf-8 -*-
import requests
url1 = "http://www.exanple.com/login"# 
url2 = "http://www.example.com/main"# 
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }
res1 = requests.post(url1, data=data, headers=headers)
res2 = requests.get(url2, cookies=res1.cookies, headers=headers)

print res2.content# 
print res2.raw# , stream=True
print res2.raw.read(50)
print type(res2.text)# unicode 
print res2.url
print res2.history# 
print res2.cookies
print res2.cookies["example_cookie_name"]
print res2.headers
print res2.headers["Content-Type"]
print res2.headers.get("content-type")
print res2.json# json
print res2.encoding# 
print res2.status_code# http 
print res2.raise_for_status()# 
(3).session 대상의 쓰기 사용하기

#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"# 
url2 = "http://www.example.com/main"# 
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }

prepped1 = requests.Request("POST", url1,
  data=data,
  headers=headers
).prepare()
s.send(prepped1)


"""
 
res = requests.Request("POST", url1,
data=data,
headers=headers
)
prepared = s.prepare_request(res)
# do something with prepped.body
# do something with prepped.headers
s.send(prepared)
"""

prepare2 = requests.Request("POST", url2,
  headers=headers
).prepare()
res2 = s.send(prepare2)

print res2.content


""" """
#-*- coding:utf-8 -*-
import requests
s = requests.Session()
url1 = "http://www.exanple.com/login"# 
url2 = "http://www.example.com/main"# 
data={"user":"user","password":"pass"}
headers = { "Accept":"text/html,application/xhtml+xml,application/xml;",
      "Accept-Encoding":"gzip",
      "Accept-Language":"zh-CN,zh;q=0.8",
      "Referer":"http://www.example.com/",
      "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"
      }
res1 = s.post(url1, data=data)
res2 = s.post(url2)
print(resp2.content)
3. 기타 요청 방식
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options(http://httpbin.org/get)
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기