import requests
import json
baiDu_response = requests.get(
'http://www.baidu.com')
baiDu_response = requests.get(
'http://www.baidu.com', timeout=
1)
print(
'无参数的get请求地址为: ' + baiDu_response.url)
print(
'返回的状态码为: '+str(baiDu_response.status_code))
print(
'返回内容编码格式为:' + baiDu_response.encoding)
print(
'Text返回的数据内容为:' + baiDu_response.text)
sendDictParams = {
'key1':
'value1',
'key2':
'value2'}
baiDu_dictParams_response = requests.get(
'http://www.baidu.com', params=sendDictParams)
print(
'普通参数的get请求地址为: ' + baiDu_dictParams_response.url)
sendListParams = {
'key1':
'value1',
'key2': [
'value2',
'value3']}
baiDu_listParams_response = requests.get(
'http://www.baidu.com', params=sendListParams)
print(
'带list参数的get请求地址为: ' + baiDu_listParams_response.url)
postResponse = requests.post(
"http://pythontab.com/postTest", data={
'key':
'value'})
print(
'普通参数请求返回状态码为:' + str(postResponse.status_code))
jsonParams = {
'key':
'value'}
postJsonResponse = requests.post(
"http://pythontab.com/postTest", data=json.dumps(jsonParams))
print(
'json参数请求返回状态码为:' + str(postJsonResponse.status_code))
files = {
'file': open(
'test.csv',
'rb')}
fileResponse = requests.post(
'http://pythontab.com/postTest', files=files)
print(
'文件参数请求返回状态码为:' + str(fileResponse.status_code))
headers_response = requests.get(
'http://www.baidu.com')
print(
'请求响应头为: ' + str(headers_response.headers))
print(
'请求响应头的Server参数 写法一:' + headers_response.headers[
'Server'])
print(
'请求响应头的Server参数 写法二:' + headers_response.headers.get(
'Server'))
headers = {
'user-agent':
'myAgent'}
custom_headers_response = requests.get(
'http://www.baidu.com', headers=headers)
print(
'自定义header发送请求状态码为:' + str(custom_headers_response.status_code))
cookies_response = requests.get(
'http://www.baidu.com')
print(
'请求地址的Cookies为: ' + str(cookies_response.cookies))
cookies = {
'user-cookies':
'myCookies'}
custom_cookies_response = requests.get(
'http://www.baidu.com', cookies=cookies)
print(
'自定义Cookies发送请求状态码为:' + str(custom_cookies_response.status_code))
proxies = {
"http":
"http://10.10.1.10:3128",
"https":
"http://10.10.1.10:1080",
}
session = requests.Session()
login_data = {
'email':
'email@example.com',
'password':
'password'}
session.post(
"http://pythontab.com/testLogin", login_data)
session_response = session.get(
'http://pythontab.com/notification/')
print(
'session请求返回的状态码为:' + str(session_response.status_code))
baiDuHtmlContent = requests.get(
"http://www.baidu.com")
with open(
"百度.html",
"wb") as
html:
html.write(baiDuHtmlContent.content)
html.close()
运行结果
参考:www.pythontab.com/html/2017/pythonjichu_0510/1138.html
转载于:https://www.cnblogs.com/bilaisheng/p/10211034.html