requests库的基本使用
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库的基本使用的更多相关文章
- Python爬虫小白入门(二)requests库
一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...
- 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 ...
- Requests库的几种请求 - 通过API操作Github
本文内容来源:https://www.dataquest.io/mission/117/working-with-apis 本文的数据来源:https://en.wikipedia.org/wiki/ ...
- python脚本实例002- 利用requests库实现应用登录
#! /usr/bin/python # coding:utf-8 #导入requests库 import requests #获取会话 s = requests.session() #创建登录数据 ...
- 大概看了一天python request源码。写下python requests库发送 get,post请求大概过程。
python requests库发送请求时,比如get请求,大概过程. 一.发起get请求过程:调用requests.get(url,**kwargs)-->request('get', url ...
- python WEB接口自动化测试之requests库详解
由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...
- python爬虫从入门到放弃(四)之 Requests库的基本使用
什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现,其 ...
- (转)Python爬虫利器一之Requests库的用法
官方文档 以下内容大多来自于官方文档,本文进行了一些修改和总结.要了解更多可以参考 官方文档 安装 利用 pip 安装 $ pip install requests 或者利用 easy_install ...
- python requests库学习笔记(上)
尊重博客园原创精神,请勿转载! requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.pytho ...
- 使用Python的requests库进行接口测试——session对象的妙用
from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
随机推荐
- 浅析Memcache和Redis
想必开发的小伙伴们对Memcache和Redis都不陌生吧,最近正好在整理它们,于是就写一下博客吧!一方面是分享,另一方面便于自己查找. 首先,来说说Memcache和Redis是什么? 说得简单一点 ...
- 【winform】userControl刷新父窗体的datagridview
1.ContextMenuStrip 获取右键控件名称 this.contextMenuScriptScore.SourceControl.Name; //当前控件名 2.radiobutton 分组 ...
- 解决使用maven的java web项目导入后出现的有关问题 -cannot be read or is not a valid ZIP file
解决使用maven的java web项目导入后出现的有关问题 -cannot be read or is not a valid ZIP file 错误问题:虽然查找repository目录下是有 ...
- SSM框架中常用的配置文件
学习框架,刚开始的时候最烦的就是一些配置文件,有很多需要配置的东西,今天把这些配置文件信息稍微整理一下,以后说不定会用的到. web.xml文件 <?xml version="1.0& ...
- 读文件/写文件。http请求。读取文件列表。
package transfor; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import j ...
- IdentityServer4 记录
IdentityServer4 文档 https://www.cnblogs.com/edisonchou/p/identityserver4_foundation_and_quickstart_01 ...
- Tornado之异步authenticated
authenticated是tornado自带的登录验证装饰器,它的实现比较简单,验证比较简易,无法做到真正意义的前后端分离并且是同步的方式,所以这里我对它进行了重写,以适应异步JWT方式的登录验证. ...
- shullfe机制详解
一.shuffle机制概述 shuffle机制就是发生在MR程序中,Mapper之后,Reducer之前的一系列分区排序的操作.shuffle的作用是为了保证Reducer收到的数据都是按键排序的. ...
- centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'
centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法 3.7版本需要一个新的包libffi-de ...
- git彻底删除某个文件的全部log历史记录
git filter-branch -f --tree-filter 'rm -rf vendor/gems' HEAD git push origin --force如果log历史较多 可能需要一点 ...