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 ...
随机推荐
- Could not create the view: An unexpected exception was thrown的解决方法
MyEclipse下面的server窗口突然不能正常显示了,而且还显示Could not create the view: An unexpected exception was thrown(无法创 ...
- Python爬取百度贴吧
from urllib import request,parseimport os#找到借口及关键字base_url = 'http://tieba.baidu.com/f?'a = input(&q ...
- Algorithm --> 两个栈实现队列和两个队列实现栈
两个栈实现队列和两个队列实现栈 队列(queue)先进先出的线性表:栈(stack)先进后出的线性表. 两个栈实现队列 法一思路: s1是入栈的,s2是出栈的. 入队列:直接压入s1即可: 出队列:如 ...
- Fiddler修改请求和响应
通过设置断点,Fiddler可以做到: 1. 修改HTTP请求头信息.例如修改请求头的UA, Cookie, Referer 信息,通过"伪造"相应信息达到达到相应的目的(调试,模 ...
- java中equals与==的区别
http://blog.csdn.net/zfrong/article/details/4290904
- 使用jmeter+ant进行接口自动化测试(数据驱动)之二:利用apache-ant执行测试用例并生成HTML格式测试报告
在 使用jmeter+ant进行接口自动化测试(数据驱动)之一 介绍了如何使用csv文件来批量管理接口 本次接着介绍如何利用apache-ant执行测试用例并生成HTML格式测试报告 ①下载安装 ap ...
- C语言第二次博客作业—分支结构
一.PTA实验作业 题目1:计算分段函数 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); print ...
- fs 创建文件夹
var http = require("http"); var fs = require("fs"); var server = http.createServ ...
- django搭建web (二) urls.py
URL模式: 在app下的urls.py中 urlpatterns=[ url(正则表达式,view函数,参数,别名,前缀)] urlpatterns=[ url(r'^hello/$',hello. ...
- "未找到应用程序的“aps-environment”的权利字符串"
1.先生成App ID,在去Provisioning里面生成新的Profile 2.删除Xcode里面原来的push profile(如果没有就不用删除)再次双击新下载的profile(mobilep ...