requests请求高德地图api

高德地图通过GET方式发送url请求数据。url里需要包含开发者key以及一些请求的具体参数。(详情可见高德官网)高德返回的数据默认为JSON格式,方便处理。

顺带来回忆一下requests模块的一些方法。

requests发送GET请求:

基本操作:

import requests
ret = requests.get('https://github.com/timeline.json')

print(ret.url)        # 打印访问的url
print(ret.text)        # 打印返回值

print(ret.content)    #打印内容,编码格式默认为Unicode
print(ret.content.decode("utf-8"))    #内容解码为utf-8
无参数get请求

有水平操作:

import requests

payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.get("http://httpbin.org/get", params=payload)

print(ret.url)        # 打印访问的url    http://httpbin.org/get?key1=value1&key2=value2
print(ret.text)       # 打印返回值
print(re.json())     #将返回的JSON数据
print(re.content)    #打印二进制的数据格式,转化为str需要decode转码


import json
print(json.loads(re.text))    
有参数get请求

 高级操作:

import requests
ret = requests.get(
   url='http://www.baidu.com',
   params={'k1':123,'k2':456},       #http://www.baidu.com?k1=123&k2=456
   cookies={'c1':'111','c2':'222'}, #requests会将这个cookie放到请求头中
   headers={                         #一般在请求头中做爬虫限制就下面三个限制
      'User-Agent':'',              # 伪造浏览器标记
      'Referer': 'http://dig.chouti.com/',
      # 有些网站在爬取时做了Referer限制,即判断上一次访问的是否是这个网站,是才让登录
      'xml-httprequest':'true',   #发送ajax请求可能就会有这个标记
   }
)
print ret.text
带请求头的get请求

requests发送POST请求:

基本用例

import requests

payload = {'key1': 'value1', 'key2': 'value2'}
ret = requests.post("http://httpbin.org/post", data=payload)

print(ret.text)
基本实例

请求头和数据实例

import requests
import json

url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}

ret = requests.post(url, data=json.dumps(payload), headers=headers)

print(ret.text)
print(ret.cookies)
请求头和数据实例

requests发送post常用参数

POST请求常用参数

requests.post发送文件

import requests
requests.post(
   url='xxx',
   files={
      'f1':open('s1.py','rb'),               #这样就可以将s1.py这个文件上传到上面url中了
      'f2':('ssss1.py',open('s1.py','rb')), 
 #指定上传文件名:第一个参数是上传到服务器端的文件名
   }
)
requests.post发送文件

requests.request()发送请求:

requests.request()常用参数

requests.request(
   method='POST',                         # 提交方式
   url='http://www.oldboyedu.com',        # 提交地址
   params={'k1': 'v1', 'k2': 'v2'},       # 在url中传递的数据:get方式
   data={'user': 'alex', 'pwd': '123'},   # 通过请求体传递数据:post方式
   # json = {'user':'alex','pwd':'123',{'k1':'v1','k2':'v2'}},
   # json和data都是通过post向请求体传递数据,但是json传递的数据可以在字典中嵌套字典
   cookies={'cook1': 'value1'},           # 发送Cookie到服务器端
   headers={
      'Referer': 'http://dig.chouti.com/', 
      # 有些网站在爬取时做了Referer限制,即判断上一次访问的是否是这个网站,是才让登录
      'User-Agent': 'Mozilla/5.0Safari/537.36',    # 伪造浏览器客户端(这里是谷歌浏览器)
   },
)
requests.request()常用参数

requests.Session()帮我们自动找到cookie携带信息自动登录

import requests
session = requests.Session()
### 1、首先登陆任何页面,获取cookie
i1 = session.get(url="http://dig.chouti.com/help/service")
### 2、用户登陆,携带上一次的cookie,后台对cookie中的 gpsd 进行授权
i2 = session.post(
    url="http://dig.chouti.com/login",
    data={
        'phone': "8618538752511",
        'password': "7481079xl",
        'oneMonth': ""
    })
### 3、这个是点赞某条新闻的url(这样就可以模仿登录点赞了)
i3 = session.post(url="http://dig.chouti.com/link/vote?linksId=15055231",)
requests.Session()

 

原文地址:https://www.cnblogs.com/ppzhang/p/9922385.html