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. sublime 配置过程

    https://www.cnblogs.com/chengqi521/p/7600379.html

  2. windows上下载redis扩展

    关于windows电脑上下载redis扩展,网站一搜一大把,但是我相信有很多小伙伴还是不知道这个扩展到底怎么下载.好了,现在我就用通俗易懂的话来告诉大家怎么下载安装这个redis扩展. 1.首先我们先 ...

  3. 【转】Java Socket编程基础及深入讲解

    原文:https://www.cnblogs.com/yiwangzhibujian/p/7107785.html#q2.3.3 Socket是Java网络编程的基础,了解还是有好处的, 这篇文章主要 ...

  4. Go的sort接口实现

    package main import ( "fmt" "sort" "time" ) type Track struct { Title ...

  5. JS浅谈原始值与引用值操作

    值的操作分为三大类:复制,传递,比较 一:复制 原始值 let a = 10; let b = a; 注释:2018-7-30 17:33:49 1 原始类型的值都是存放在栈内存当中,所以他们的赋值操 ...

  6. Auth模块、Forms组件

    Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...

  7. Urozero Autumn 2016. NCPC 2016

    A. Artwork 倒过来并查集维护即可. #include<cstdio> #include<algorithm> using namespace std; const i ...

  8. Vue(二十五)打包后路径报错问题

    1.修改 config - index.js 2.修改 build - utils.js

  9. __x__(9)0906第三天__常见的标签

    <!doctype html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. 20181207_Second_小结

    1. 上下 200*200 盒子的重叠,切记用 absolute 绝对定位 为最佳解决方案 2. 移动端多使用 粘连布局 <!DOCTYPE html> <html> < ...