python requests get 参数_python+requests——不带参数的get请求

importrequests

url= "http://www.baidu.com" #请求网址

yctime= 0.05 #不能超过的时间

resp= requests.get(url,timeout=yctime)print("#-----------------------------------------------------------------------------------------------1")print(type(resp.url)) #返回一个rul类型,说明返回的是一个字符串:

print(resp.url) #访问网址:http://www.baidu.com/

print("#-----------------------------------------------------------------------------------------------2")print(type(resp.history)) #返回的是一个列表类型:

print(resp.history) #返回空[] ,说明并没有被重定向 打印请求的历史记录(以列表的形式显示)

print("#-----------------------------------------------------------------------------------------------3")print(type(resp.status_code)) #返回的是一个整数类型:

print(resp.status_code) #返回状态码:200 判断是否访问正常

print("#-----------------------------------------------------------------------------------------------4")if (resp.status_code ==requests.codes.ok):print("请求成功")else:print("失败") #返回:请求成功

if (resp.status_code == 200):print("请求成功")else:print("失败") #返回:请求成功

print("#-----------------------------------------------------------------------------------------------5")print(resp.apparent_encoding) #返回:utf-8 从内容中分析出的响应内容编码方式(备选编码方式)

print(resp.encoding) #返回当前的编码格式:ISO-8859-1 #从HTTP header中猜测的响应内容编码方式

resp.encoding= 'utf-8' #设置编码格式 #response.encoding=“utf-8” 或者 response.encoding=”gbk”具体看网页什么方式编码的

#如果能正常显示就不要设置编码格式了,正常输出

print(resp.encoding) #返回:utf-8 说明上一步设置成功

print(resp.content) #返回的是 bytes 型的二进制数据,如果想取图片,文件,则可以通过 response.content

#更改编码使用 response.content.deocde(“utf8”)

#: 返回的数据类型

#: response.text 返回的是一个 unicode 型的文本数据

#: response.content 返回的是 bytes 型的二进制数据

#: 也就是说如果想取文本数据可以通过response.text 如果想取图片,文件,则可以通过 response.content

#: response.text返回的是Unicode格式,通常需要转换为utf-8格式,否则就是乱码。#

#response.content是二进制模式,可以下载视频之类的,如果想看的话需要decode成utf-8格式。

#: 不管是通过response.content.decode("utf-8)的方式还是通过response.encoding="utf-8"的方式都可以避免乱码的问题发生

#:response.apparent_encoding 从内容中分析出的响应内容编码方式(备选编码方式)

print("#-----------------------------------------------------------------------------------------------6")#{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive',#'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 21 Feb 2020 17:06:24 GMT',#'Last-Modified': 'Mon, 23 Jan 2017 13:28:23 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18',#'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}

print(resp.headers) #返回响应头部,如上所示

print(resp.headers['content-encoding']) #返回:gzip

print(resp.headers['Content-type']) #返回:text/html

print(resp.headers.get('Content-type'))#返回:text/html

print(resp.headers['date']) #返回:Fri, 21 Feb 2020 17:06:24 GMT

print("#-----------------------------------------------------------------------------------------------7")#]>

print(resp.cookies) #返回一个cookie,具体如上

#print(resp.cookies['name']) #设置name参数,即可访问到具体的值

print("#-----------------------------------------------------------------------------------------------8")print(type(resp.text)) #返回一个str类型:

print(resp.text) #返回请求的响应体

执行结果:

#-----------------------------------------------------------------------------------------------1

http://www.baidu.com/

#-----------------------------------------------------------------------------------------------2

[]

#-----------------------------------------------------------------------------------------------3

200

#-----------------------------------------------------------------------------------------------4

请求成功

请求成功

#-----------------------------------------------------------------------------------------------5

utf-8

ISO-8859-1

utf-8

b'\r\n

\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b\xef\xbc\x8c\xe4\xbd\xa0\xe5\xb0\xb1\xe7\x9f\xa5\xe9\x81\x93 \r\n'

#-----------------------------------------------------------------------------------------------6

{'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Connection': 'keep-alive', 'Content-Encoding': 'gzip', 'Content-Type': 'text/html', 'Date': 'Fri, 21 Feb 2020 17:15:21 GMT', 'Last-Modified': 'Mon, 23 Jan 2017 13:28:24 GMT', 'Pragma': 'no-cache', 'Server': 'bfe/1.0.8.18', 'Set-Cookie': 'BDORZ=27315; max-age=86400; domain=.baidu.com; path=/', 'Transfer-Encoding': 'chunked'}

gzip

text/html

Fri, 21 Feb 2020 17:15:21 GMT

#-----------------------------------------------------------------------------------------------7

]>

#-----------------------------------------------------------------------------------------------8

百度一下,你就知道
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值