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

import  requests

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

yctime = 0.05            #不能超过的时间

resp = requests.get(url,timeout=yctime)

print("#-----------------------------------------------------------------------------------------------1")

print(type(resp.url))  #返回一个rul类型,说明返回的是一个字符串:<class ‘str‘>

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

print("#-----------------------------------------------------------------------------------------------2")

print(type(resp.history))   #返回的是一个列表类型:<class ‘list‘>

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

print("#-----------------------------------------------------------------------------------------------3")

print(type(resp.status_code))   #返回的是一个整数类型:<class ‘int‘>

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")

#<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>

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

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

print("#-----------------------------------------------------------------------------------------------8")

print(type(resp.text))           #返回一个str类型: <class ‘str‘>

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

执行结果:

#-----------------------------------------------------------------------------------------------1
<class ‘str‘>
http://www.baidu.com/
#-----------------------------------------------------------------------------------------------2
<class ‘list‘>
[]
#-----------------------------------------------------------------------------------------------3
<class ‘int‘>
200
#-----------------------------------------------------------------------------------------------4
请求成功
请求成功
#-----------------------------------------------------------------------------------------------5
utf-8
ISO-8859-1
utf-8
b‘<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>\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</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class="fm"> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class="s_ipt" value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=\xe7\x99\xbe\xe5\xba\xa6\xe4\xb8\x80\xe4\xb8\x8b class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">\xe6\x96\xb0\xe9\x97\xbb</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">\xe5\x9c\xb0\xe5\x9b\xbe</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">\xe8\xa7\x86\xe9\xa2\x91</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">\xe8\xb4\xb4\xe5\x90\xa7</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class="lb">\xe7\x99\xbb\xe5\xbd\x95</a> </noscript> <script>document.write(\‘<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\‘+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ \‘" name="tj_login" class="lb">\xe7\x99\xbb\xe5\xbd\x95</a>\‘);</script> <a href=//www.baidu.com/more/ name=tj_briicon class="bri" style="display: block;">\xe6\x9b\xb4\xe5\xa4\x9a\xe4\xba\xa7\xe5\x93\x81</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>\xe5\x85\xb3\xe4\xba\x8e\xe7\x99\xbe\xe5\xba\xa6</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>\xe4\xbd\xbf\xe7\x94\xa8\xe7\x99\xbe\xe5\xba\xa6\xe5\x89\x8d\xe5\xbf\x85\xe8\xaf\xbb</a>&nbsp; <a href=http://jianyi.baidu.com/ class="cp-feedback">\xe6\x84\x8f\xe8\xa7\x81\xe5\x8f\x8d\xe9\xa6\x88</a>&nbsp;\xe4\xba\xacICP\xe8\xaf\x81030173\xe5\x8f\xb7&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>\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
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
#-----------------------------------------------------------------------------------------------8
<class ‘str‘>
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class="head_wrapper"> <div class="s_form"> <div class="s_form_wrapper"> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class="fm"> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class="s_ipt" value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class="mnav">新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class="mnav">hao123</a> <a href=http://map.baidu.com name=tj_trmap class="mnav">地图</a> <a href=http://v.baidu.com name=tj_trvideo class="mnav">视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class="mnav">贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class="lb">登录</a> </noscript> <script>document.write(‘<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=‘+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ ‘" name="tj_login" class="lb">登录</a>‘);</script> <a href=//www.baidu.com/more/ name=tj_briicon class="bri" style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class="cp-feedback">意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

原文地址:https://www.cnblogs.com/xiaobaibailongma/p/12343932.html

时间: 2024-08-30 03:20:29

python+requests——不带参数的get请求的相关文章

python+requests——发送带参数的get请求

Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数. 举例来说,如果你想传递 key1=value1 和 key2=value2 到 httpbin.org/get ,那么你可以使用如下代码: payload = {'key1': 'value1', 'key2': 'value2'} resp = requests.get("http://httpbin.org/get", params=payload,timeout=0.5) 原文地址:http

python+requests实现接口测试 - get与post请求使用

简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求.Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner.更重要的一点是它支持 Python3 哦! 一.安装 使pip安装: pip install requests 安装完后,运行一个简单的例子查看是否安装成功: impo

python中编写带参数decorator

考察上一节的 @log 装饰器: def log(f): def fn(x): print 'call ' + f.__name__ + '()...' return f(x) return fn 发现对于被装饰的函数,log打印的语句是不能变的(除了函数名). 如果有的函数非常重要,希望打印出'[INFO] call xxx()...',有的函数不太重要,希望打印出'[DEBUG] call xxx()...',这时,log函数本身就需要传入'INFO'或'DEBUG'这样的参数,类似这样:

发送带参数的get请求,并解决httpclient编码问题

package cn.wemart.httppost; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList;

HttpClient 的GET(带参数)、POST请求方式,工具类方法

/** * 连接/断开操作 post方式 * @param url * @param json */ private boolean connOrDisconnOperator(String url,String json){ CloseableHttpClient client = null; CloseableHttpResponse response = null; boolean flag = false; try{ HttpPost httpPost = new HttpPost(ur

Python(10)_带参数的url的拼接方式

import urllib.parse url = 'http://www.baidu.com/index.html'; data = { 'name':'bowen', 'age':25, 'sex':'male', } lt = [] for k,v in data.items(): lt.append(k+'='+str(v)) # 这个是在for循环外面的,就是将列表的元素之间用&符号连接起来 query_string = '&'.join(lt) print(lt) print(

python+requests——发送post请求——各种情况

requests库是 python3 中非常优秀的第三方库,它使用 Apache2 Licensed 许可证的 HTTP 库,用 Python 编写,真正的为人类着想. requests 使用的是 urllib3(python3.x中的urllib),因此继承了它的所有特性. Requests 会自动实现持久连接keep-alive,Requests 支持 HTTP 连接保持和连接池,支持使用 cookie 保持会话,支持文件上传, 支持自动确定响应内容的编码,支持国际化的 URL 和 POST

Python+Requests接口测试教程(2):requests

开讲前,告诉大家requests有他自己的官方文档:http://cn.python-requests.org/zh_CN/latest/ 2.1 发get请求 前言requests模块,也就是老污龟,为啥叫它老污龟呢,因为这个官网上的logo就是这只污龟,接下来就是学习它了.环境准备(小编环境):python:2.7.12pycharm:5.0.4requests:2.13.0(这学本篇之前,先要有一定的python基础,因为后面都是直接用python写代码了,小编主要以讲接口为主,pytho

python中requests库get方法带参数请求

起因是想爬五等分的花嫁的漫画.这是其中的一个坑 先上代码 data={ 'cid':567464, 'page':1, 'key':'', 'language':1, 'gtk':6, '_cid':567464, '_mid':34949, '_dt':'2019-05-03 13:03:08', '_sign':'e74c8c52618a64a454dd7f12aff3cc1c' }def getFun(url,data): ret=requests.get(url,params=data)