requests-get请求
import requests
response= requests.get('http://www.baidu.com')#get方法请求网址
print(response)
print(response.status_code)#状态码
print(response.text)#响应体
print(response.cookies)#获取cookies
另外还有response.url,response.history历史记录
#requests的各种请求方式
import requests
requests.get('http://httpbin.org/get')
requests.post('http://httpbin.org/post')
requests.delete('http://httpbin.org/delete')
requests.head('http://httpbin.org/head')
requests.options('http://httpbin.org/options')
#简单的get请求
#通过response.text获得响应体
import requests
response = requests.get('http://httpbin.org/get')
print(response.text) #带参数的请求
#利用params将字典形式数据传入进去,相当于urllib.parse.urlencode
data = {
'name':'germy',
'age':22
}
response = requests.get('http://httpbin.org/get',params=data)
print(response.text)
#解析json
#response.json()相当于json.loads()方法
import requests
import json
response = requests.get('http://httpbin.org/get')
print(response.json())
print('*'*100)
print(json.loads(response.text))
#获取并保存二进制数据,response.content即二进制数据
import requests
response= requests.get('http://inews.gtimg.com/newsapp_ls/0/1531939223/0')
#print(response.content)
with open('D://tomas.jpg','wb') as f:
f.write(response.content)
#添加headers
import requests
response = requests.get('https://www.zhihu.com/explore')
#print(response.text)#结果返回服务器端错误,证实爬虫被知乎禁止了
#结果:<html><body><h1>500 Server Error</h1> #解决的方法是添加headers,方法非常简单,加进去就可以了
headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 LBBROWSER'}
response = requests.get('https://www.zhihu.com/explore',headers=headers)
print(response.text)
requests-get请求的更多相关文章
- 接口自动化测试遭遇问题,excel中取出来的json串,无法使用requests去请求解决办法
最近遭遇了一个问题,问题不大不小,想半天没想明白是哪里有问题,今天终于解决了 用python读取了excel用例中,body json字符串内容,然后requests去请求内容,结果一直报错,一直不明 ...
- selenium登录163邮箱,得到cookie,requests后续请求
1.场景 很多时候登录操作是比较复杂的,因为存在各种反爆破操作,以及为了安全性提交数据都会存在加密.如果要完全模拟代码去实现登录操作是比较复杂,并且该网站后续更新了登录安全相关功能,那么登录的模拟操作 ...
- Python使用requests发送请求
Python使用第三方包requests发送请求,实现接口自动化 发送请求分三步: 1.组装请求:包括请求地址.请求头header.cookies.请求数据等 2.发送请求,获取响应:支持get.po ...
- python爬虫 - python requests网络请求简洁之道
http://blog.csdn.net/pipisorry/article/details/48086195 requests简介 requests是一个很实用的Python HTTP客户端库,编写 ...
- Requests模块—请求
1. 安装 pip install requests import requests 2. 使用 (1) GET 1. 语法 requests.get(url, params=None, **kwar ...
- python requests http请求
导入模块 import requests import json header = {'Content-Type': 'application/json'} data = {"} data ...
- 【Python】requests.post请求注册实例
#encoding=utf-8 import requests import json import time import random import multiprocessing from mu ...
- Requests库请求网站
安装requests库 pip install requests 1.使用GET方式抓取数据: import requests #导入requests库 url="http://www.cn ...
- Python爬虫requests判断请求超时并重新发送请求
下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html 在爬虫的执行当中,总会遇到请求连接 ...
- python3基础04(requests常见请求)
#!/usr/bin/env python# -*- coding:utf-8 -*- import requestsimport jsonimport reimport urllib3from ur ...
随机推荐
- ELK学习笔记(一)安装Elasticsearch、Kibana、Logstash和X-Pack
最近在学习ELK的时候踩了不少的坑,特此写个笔记记录下学习过程. 日志主要包括系统日志.应用程序日志和安全日志.系统运维和开发人员可以通过日志了解服务器软硬件信息.检查配置过程中的错误及错误发生的原因 ...
- opencv配置
1.安装opecv库 从官网http://opencv.org/下载OpenCV windows版 运行之后 2.配置环境变量 64位系统,可以将32位和64位两个都添加上,免得以后编译不同程序再来配 ...
- vue-过渡动画
本篇资料参考于官方文档: http://cn.vuejs.org/guide/transitions.html 概述: Vue 在跳转页面时,提供多种不同方式的动画过渡效果. ●in-out:新元素先 ...
- 推荐一个利用 python 生成 pptx 分析报告的工具包:reportgen
reportgen v0.1.8 更新介绍 这段时间,我对 reportgen 进行了大工程量的修改和更新.将之前在各个文章中出现的函数进行了封装,同时也对现有工具包的一些逻辑进行了调整. 1.rep ...
- Go语言标准库_输入/输出
Go语言标准库_输入/输出 转载节选自<Go语言标准库> Reader 接口 type Reader interface { Read(p []byte) (n int, err erro ...
- 记录python接口自动化测试--unittest框架基本应用(第二目)
在第一目里写了几个简单demo,并把调用get和post请求的方法封装到了一个类里,这次结合python自带的unittest框架,用之前封装的方法来写一个接口测试demo 1.unittest简单用 ...
- 20162308 实验二《Java面向对象程序设计》实验报告
20162308 实验二<Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 ...
- io多路复用(三)
#!/usr/bin/env python # -*- coding:utf-8 -*- import socket sk1 = socket.socket() sk1.bind(('127.0.0. ...
- js 选择图片生成base64数据
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- Vue-cli+Vue.js2.0+Vuex2.0+vue-router+es6+webpack+node.js脚手架搭建和Vue开发实战
Vue.js是一个构建数据驱动的web界面的渐进式框架.在写这边文章时Vue版本分为1.0++和2.0++,这个是基于Vue2.0的项目. Vue-cli是构建单页应用的脚手架,这个可是官方的. Vu ...