python day3

  1. 讲解内容:requests 库的基础使用、实例

  2. 引入requests库

  3. 调用get方法,需要传入请求的url地址

  4. 输出响应内容(可以获取服务器返回的http头部信息,http响应内容,响应码等)

  5. 第二个方法为实例内容(可以获取对应地址的经纬度)

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# requests 库的基本使用

import requests		#引入requests库


url="https://www.baidu.com"

def getMethod(url):
	html=requests.get(url)		#通过get方法发送请求到指定的url
	print(html.status_code)		#获取响应码
	print(html.encoding)		#默认编码  ISO-8859-1
	html.encoding='utf-8'		#设置新的编码,避免输出乱码
	print(html.text)		#输出服务器返回的内容
	print(html.headers)			#打印http头部信息
	print(html.headers['Server'])		#获取指定http头部字段,不区分大小写
	print(html.headers['server'])		#获取指定http头部字段,不区分大小写
	pass

# 获取地址对应经纬度
def getLocation():
	address="上海市东方明珠"
	url="https://apis.map.qq.com/ws/geocoder/v1/?address={}&key=H3IBZ-IHNWI-I55G4-5XHG3-LKKDO-TUFUX".format(address)
	html=requests.get(url)
	print(html.status_code)
	print(html.text)
	pass



if __name__ == '__main__':

	getMethod(url)
	print("""+++++++++++++++++++++++"""*5)
	getLocation()

 

实例结果运行

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿雷由

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值