2020年7月26日

python http get post整理

作者 spoony
仅使用requests
普通get方式:
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
普通post方式:
import requests
data = {
"name":"zhaofan",
"age":23
}
response = requests.post("http://httpbin.org/post",data=data)
print(response.text)
带head的get:
import urllib2
request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE','application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()
带head的post:
import urllib2
import urllib
request = urllib2.Request("http://passport.baidu.com/login.aspx")
request.add_header('content-TYPE''application/x-www-form-urlencoded')
data={"tbUserName":"test_username""tbPassword":"test_password"}
response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()
如果带cookies,可以请求头里加入cookies

若想让多次请求共有一个session,可在请求头加入cookies信息:

#  -*- coding: utf-8 -*-
import urllib2
user_agent = r'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400'
headers = {r'User-Agent': user_agent}
url = "http://localhost:80/webtest/test"
req = urllib2.Request(url, headers = headers)
response = urllib2.urlopen(req)
cookie = response.headers.get('Set-Cookie')    # 从第一次的请求返回中获取cookie
print cookie
page_html = response.read()
print page_html

req.add_header('cookie', cookie)    # 将cookie加入以后的请求头,保证多次请求属于一个session
response = urllib2.urlopen(req)
page_html = response.read()
print page_html

也可用到cookielib库:注意py3改成cookiejar

import urllib2
import cookielib

url = ‘http://www.baidu.com/’
cookies = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookies)
opener = urllib2.build_opener(handler)
response = opener.open(url)

for cookie in cookies:
print “%s: %s” % (cookie.name, cookie.value)