requests简单应用

  • requests发送get请求
  1. 无参数的get请求:
1 response = requests.get(url='http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getRegionProvince')
2 s = response.status_code

2.有参数的get请求

1 url = 'http://ws.webxml.com.cn/WebServices//WeatherWS.asmx/getSupportCityString'
2 params= {'theRegionCode':3113}
3 response = requests.get(url = url,params=params)
  • requests发送post请求

1 """
2 post请求常见的四种方式
3     1.application/x-www-form-urlencoded :最常见的post提交数据的方式,浏览器的原生<form>表单
4     2.form-data :一般用来上传文件
5     3.application/json :作为请求头,用来告诉服务端消息主体是序列化的JSON字符串
6     4.text/xml 
7 """
    1. 1.application/x-www-form-urlencoded
1 url = 'http://ws.webxml.com.cn/WebServices//WeatherWS.asmx/getSupportCityString'
2 params = {'theRegionCode':3113}
3 response = requests.post(url=url,data=params,headers={'Content-Type':'application/x-www-form-urlencoded'})

2.text/xml

 1 url = 'http://ws.webxml.com.cn/WebServices/WeatherWS.asmx'
 2 # 存入xml 为了保持文本格式
 3 data = '''<?xml version="1.0" encoding="utf-8"?>
 4 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 5   <soap:Body>
 6     <getSupportCityDataset xmlns="http://WebXml.com.cn/">
 7       <theRegionCode>3113</theRegionCode>
 8     </getSupportCityDataset>
 9   </soap:Body>
10 </soap:Envelope>'''
11 response = requests.post(url=url,data=data,headers={'Content-Type':'application/json'})
12 print(response.text)

3.application/json

1 url = 'http://139.199.132.220:8000/event/weather/getWeather/'
2 data = {"theCiryCode": 1}
3 response = requests.post(url=url,json=data,headers={'Content-Type':'text/xml'})
4 print(response.text)

4.form-data(表单提交)

1 data = {'key1':'value1','key2':'value2'}
2 requests.post(url,data=data)

 

原文地址:https://www.cnblogs.com/jiyanjiao-702521/p/10062372.html