1.发送get请求

import requests

# response=requests.get('http://www.baidu.com')
# 查看响应内容,返回的是已经解码的内容
# response.text 服务器返回的数据,已解码。解码类型:根据HTTP头部对响应的编码做出有根据的推测,推测的文本编码
# print(type(response.text))
# print(response.text)
# 百度返回的text有乱码,说明解码猜测的编码方式不对 # 查看响应内容
# print(type(response.content))
# print(response.content.decode('utf-8'))
# 解码正确,没有乱码 # 查看完整url地址
# print(response.url) # 查看响应头部字符编码
# print(response.encoding) # 查看响应码
# print(response.status_code) params = {'wd': '中国'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
#params传入,会自动进行编码
response=requests.get('http://www.baidu.com/s',headers=headers,params=params)
print(response.url)
with open('baidu.html','w',encoding='utf-8') as f:
f.write(response.content.decode('utf-8'))

2.发送post请求

import requests

data = {
'first': True, 'pn': 1, 'kd': 'python'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
'Cookie':'JSESSIONID=ABAAABAAAFCAAEGE19E4DE9949656042D040782B344E314; SEARCH_ID=94069a753d8a4157a4b8a44284d4b719; user_trace_token=20190404002147-ba37c7c8-aa84-4a31-8171-738e6bcfadf2; X_HTTP_TOKEN=42daf4b72327b2817058034551bf5e71415983ed09'
} response = requests.post('https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false',headers=headers,data=data)
print(response.text)
#把传来的数据转成原本数据类型(如果传输的是json格式的字符串)
# print(response.json()) #测试
# ret=response.text
# # import json
# # ret=json.loads(ret)
# # print(type(ret))

3.使用代理

import requests

#不使用代理
# response=requests.get('http://httpbin.org/ip')
# print(response.text) #使用代理
#尽量使用高匿名的代理,透明的话,它依然能识别原来的ip地址。
proxy={'http':'112.85.149.79:9999'}
response=requests.get('http://httpbin.org/ip',proxies=proxy)
print(response.text)

4.处理cookie信息

import requests

# response=requests.get('http://www.baidu.com')
#返回的是一个对象
# print(response.cookies)
#获取字典形式信息
# print(response.cookies.get_dict()) #session
#之前使用的urlib库,是可以使用opener发送多个请求,多个请求之间是可以共享cookie的。那么如果使用requests,
#也要达到共享cookie的目的,那么可以使用requests库提供的session对象。它简化了我们每次模拟请求时都要带上cookie
#的复杂操作,使用session它自己会帮我们带上headers里面的cookie信息
url='http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019341135380'
session=requests.session()
data={
'email':'9@qq.com',
'password':'pythonspr'
}
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
session.post(url,data=data,headers=headers)
#只有登录后才能查看大鹏的页面
response=session.get('http://www.renren.com/880151247/profile',headers=headers)
with open('renren.html','w',encoding='utf-8') as f:
f.write(response.text)
#查看页面,确实登录成功

5.处理不信任的ssl证书

#处理不信任的ssl证书,加上verify=False就可以了
import requests
resp=requests.get('http://www.12306.cn',verify=False)
print(resp.text)

requests库的基本使用的更多相关文章

  1. Python爬虫小白入门(二)requests库

    一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...

  2. Requests库上传文件时UnicodeDecodeError: 'ascii' codec can't decode byte错误解析

    在使用Request上传文件的时候碰到如下错误提示: 2013-12-20 20:51:09,235 __main__ ERROR 'ascii' codec can't decode byte 0x ...

  3. Requests库的几种请求 - 通过API操作Github

    本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...

  4. python脚本实例002- 利用requests库实现应用登录

    #! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...

  5. 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。

    python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...

  6. python WEB接口自动化测试之requests库详解

    由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...

  7. python爬虫从入门到放弃(四)之 Requests库的基本使用

    什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...

  8. (转)Python爬虫利器一之Requests库的用法

    官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...

  9. python requests库学习笔记(上)

    尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...

  10. 使用Python的requests库进行接口测试——session对象的妙用

    from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...

随机推荐

  1. 如何连接LINUX服务器

    1.WINDOW下连接 使用PUTTY连接,链接如下:https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html 下载安装后打开,运行 ...

  2. 微信小程序换皮肤,动态切换菜单栏和导航栏的样式,动态修改TabBar和NavigationBar

    在做微信小程序换皮肤的时候,需要动态修改菜单栏(TabBar)和导航栏(NavigationBar) 但是在小程序中它们的样式是写在app.json里面,而且app.json是静态编译,运行时哪怕你修 ...

  3. (初)Knockout 监控属性(Observables)

    1 创建带有监控属性的view model 1.1 Observables Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tr ...

  4. Expression Trees 参数简化查询

    ASP.NET MVC 引入了 ModelBinder 技术,让我们可以在 Action 中以强类型参数的形式接收 Request 中的数据,极大的方便了我们的编程,提高了生产力.在查询 Action ...

  5. Tomcat目录结构详解

    1.bin: 该目录下存放的是二进制可执行文件,如果是安装版,那么这个目录下会有两个exe文件:tomcat6.exe.tomcat6w.exe,前者是在控制台下启动Tomcat,后者是弹出UGI窗口 ...

  6. 在VS2010上安装MVC4(webApi)

    我们安装的VS2010上是没有MVC4或者WebApi的,要想加入这些功能只能自己在网上下载安装. 要安装MVC4,首先得安装VS10sp(Service Package)1,然后再安装MVC4.安装 ...

  7. 潭州课堂25班:Ph201805201 tornado 项目 第十课 深入应用异步和协程(课堂笔记)

    tornado 相关说明 需求: 增加 /save 的 handler,实现异步保存指定 URL 图片的功能 从网页上得到一张图片地址,由这个地址将图片保存到服务器,并将相关数据保存到数据库 impo ...

  8. arcgis中转换netCDF为栅格数据

    最近有个同学询问我一个问题,使用arcpy把netcdf转化成栅格文件,忙活了两个小时才搞定,其实主要代码非常简单,只不过要对arcgis 的功能比较熟悉(其实多思考和查考它的帮助文章,无聊) # - ...

  9. Selenium 2自动化测试实战

    Selenium 2自动化测试实战 百度网盘 链接:https://pan.baidu.com/s/1aiP3d8Y1QlcHD3fAlEj4sg 提取码:jp8e 复制这段内容后打开百度网盘手机Ap ...

  10. X Open Cup named after E.V. Pankratiev. European Grand Prix

    A. Arithmetic Rectangle 对于一行或者一列的情况可以递推求出最大值. 对于至少一行或者一列的情况,可以定义四个格子一组横向和纵向的相等关系,然后悬线法求最大子矩阵. 时间复杂度$ ...