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请求的更多相关文章

  1. 接口自动化测试遭遇问题,excel中取出来的json串,无法使用requests去请求解决办法

    最近遭遇了一个问题,问题不大不小,想半天没想明白是哪里有问题,今天终于解决了 用python读取了excel用例中,body json字符串内容,然后requests去请求内容,结果一直报错,一直不明 ...

  2. selenium登录163邮箱,得到cookie,requests后续请求

    1.场景 很多时候登录操作是比较复杂的,因为存在各种反爆破操作,以及为了安全性提交数据都会存在加密.如果要完全模拟代码去实现登录操作是比较复杂,并且该网站后续更新了登录安全相关功能,那么登录的模拟操作 ...

  3. Python使用requests发送请求

    Python使用第三方包requests发送请求,实现接口自动化 发送请求分三步: 1.组装请求:包括请求地址.请求头header.cookies.请求数据等 2.发送请求,获取响应:支持get.po ...

  4. python爬虫 - python requests网络请求简洁之道

    http://blog.csdn.net/pipisorry/article/details/48086195 requests简介 requests是一个很实用的Python HTTP客户端库,编写 ...

  5. Requests模块—请求

    1. 安装 pip install requests import requests 2. 使用 (1) GET 1. 语法 requests.get(url, params=None, **kwar ...

  6. python requests http请求

    导入模块 import requests import json header = {'Content-Type': 'application/json'} data = {"} data ...

  7. 【Python】requests.post请求注册实例

    #encoding=utf-8 import requests import json import time import random import multiprocessing from mu ...

  8. Requests库请求网站

    安装requests库 pip install requests 1.使用GET方式抓取数据: import requests #导入requests库 url="http://www.cn ...

  9. Python爬虫requests判断请求超时并重新发送请求

     下面是简单的一个重复请求过程,更高级更简单的请移步本博客: https://www.cnblogs.com/fanjp666888/p/9796943.html  在爬虫的执行当中,总会遇到请求连接 ...

  10. python3基础04(requests常见请求)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import requestsimport jsonimport reimport urllib3from ur ...

随机推荐

  1. struts2.0简单教程

    Struts2.0简单配置教程: 在Eclipse中配置Struts2 步骤一:首先打开java ee并建立一个动态网站项目,我建立的项目名为TestDemo,如下图: 建立之后可在左侧发现工程,展开 ...

  2. 基于hi-nginx的web开发(python篇)——utf-8编码

    一致地utf-8编码,非常重要.对python2而言,尤其如此. 如果在hi-nginx中使用的是python2,同时又需要无障碍地使用中日韩等文字,那么一定不要忘记使用: #-*- coding:u ...

  3. 【Python】 html解析BeautifulSoup

    BeautifulSoup bs是个html解析模块,常用来做爬虫? ■ 安装 BeautifulSoup可以通过pip来安装,用pip install beautifulsoup4 即可.但是仅仅这 ...

  4. centos文件系统变为只读的解决处理

    简单粗暴:重启 Linux系统重启或无故变为只读造成网站无法正常访问的简单临时的做法: 一. 1.mount: 用于查看哪个模块输入只读,一般显示为: /dev/hda1 on / type ext3 ...

  5. Redis这些知识点,是必须知道的!

    Redis是一个开源(BSD许可)的内存数据结构存储,可作为数据库,缓存和消息队列.相比Memcached它支持更多的数据结构,如string(字符串),hash(哈希),list(链表),set(集 ...

  6. 大数据 --> 一致性Hash算法

    一致性Hash算法 一致性Hash算法(Consistent Hash)

  7. java.lang的详细解读

    软件包    java.lang 提供java编程语言实现程序设计的基础类 接口摘要 1> appendable  提供被添加char序列和值的对象 2>charSquence char值 ...

  8. 玩转接口测试工具fiddler 教程系列1

    我们在做web测试的时候,很多时候需要查看接口发送的数据返回的数据是否正常,这样可以排除是客户端的问题还是服务器的问题,举个例子来说,如果我们发现页面上面数据少了, 通过fiddler查看数据返回就少 ...

  9. javaScript设计模式 -- 灵活的javaScript语言

    因为好长时间的懒惰和懈怠,好久没有更新文章了,从现在开始我会按时更新一些自己总结的一些知识,和研究的东西,希望能让大家从我这里学到一点点的知识. 本文参考了张荣铭的javascript设计模式一书,算 ...

  10. C#编程语言之委托与事件(一)—— C/C++函数指针和C#委托初步

    相信正在学习C#的人都有学习过C或C++的经验,本文要讲的第一个要点是C#中的委托(delegate,有些资料也叫代表).什么是委托,很多人都能自然而然地想到C/C++中的函数指针,事实上很多书和资料 ...