#coding:utf-8
# 导入requests
import requests # 构建url
url = 'http://www.baidu.com' # 发送请求,获取响应
# response = requests.get(url)
response = requests.head(url) # 检查状态码
# print (response.status_code) # 检查url
# print (response.url) # 检查请求头
# print (response.request.headers) # 检查响应头
# print (response.headers) # 检查源码
# print (response.content)
# print (response.content.decode())
#
# response.encoding='utf-8'
# print (response.text)
# print (response.encoding)

  带headers的请求

#coding:utf-8
import requests
import time # 构建url
url = 'http://www.baidu.com' # 构建请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
} # 发送请求
response = requests.get(url, headers=headers)
print (len(response.content)) time.sleep(2)
response1 = requests.get(url)
print (len(response1.content))

  

带get传参的请求

#coding:utf-8
import requests # 构建url
url = 'https://www.baidu.com/s'
# 构建headers
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
} # 构建参数
params = {
"wd": "深圳"
} # 发送请求
response = requests.get(url, headers=headers, params=params) # 验证url
# print(response.url) with open('baidu.html','w')as f:
f.write(response.content.decode())

get带有代理的请求:

#coding:utf-8
import requests # 构建一个url
url = 'http://www.itcast.cn'
# 构建headers
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36'
}
# 构建代理
# proxies = {
# "http": "http://106.14.51.145:8118",
# "https": "https://106.14.51.145:8118",
# }
# 付费代理
proxies = {
"http": "http://morganna_mode_g:ggc22qxp@117.48.199.230:16816",
"https": "https://morganna_mode_g:ggc22qxp@117.48.199.230:16816",
} # 发送请求
response = requests.get(url,headers=headers,proxies=proxies) #? 如何验证代理是否使用成功
# 运用超时抛出异常来判断是否成功,一般0.5-1秒
# response = requests.get(url, timeout=3)

  

requests库的get请求(加上head,加上get参数请求)的更多相关文章

  1. 【python接口自动化】- 使用requests库发送http请求

    前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...

  2. 使用Python的requests库进行接口测试——session对象的妙用

    from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...

  3. requests库写接口测试框架初学习

    学习网址:    https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dscpm/ff75b907-415d-4220-89 ...

  4. [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍

    [根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写  文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...

  5. Python爬虫:HTTP协议、Requests库

    HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...

  6. 【Python爬虫】Requests库的基本使用

    Requests库的基本使用 阅读目录 基本的GET请求 带参数的GET请求 解析Json 获取二进制数据 添加headers 基本的POST请求 response属性 文件上传 获取cookie 会 ...

  7. Python Requests库简单入门

    我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...

  8. 爬虫1.1-基础知识+requests库

    目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...

  9. Python爬虫:HTTP协议、Requests库(爬虫学习第一天)

    HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...

  10. requests库的使用、安装及方法的简单介绍

    requests库的使用.安装及方法的简单介绍 1.requests库的概述 requests库是一个简洁且简单的处理HTTP请求的第三方库,是公认的最好获得第三方信息的库. requests库更多信 ...

随机推荐

  1. gozmq的安装与使用

    1. 安装zmq 下载Windows版安装或linux版本并执行安装命令: tar zxvf zeromq-4.1.6.tar.gz cd zeromq-4.1.6 ./configure make ...

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  3. pom.xml文件错误

    刚创建的maven项目,马上pom.xml的第一行就报错这是第一行:<project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  4. apache使某目录下的文件能够列表显示出来

    想要使web目录下,某目录下的文件列表显示而不是显示"You don't have permission to access / on this server" 需要在httpd. ...

  5. Matlab 图像处理入门

    一些(形态学)图像处理方法如下: I = imread('pic.jpg'); I2 = rgb2gray(I); figure,imshow(I); title('原图'); figure,imsh ...

  6. 【前端积累】javascript事件

    什么是事件? 事件是一种异步编程的实现方式,本质上是程序各个组成部分之间的通信.就是文档或浏览器窗口发生的一些特定的交互瞬间(某种动作). 1.事件流 事件流描述的是从页面中接收事件的顺序. 1)事件 ...

  7. LeetCode——Implement Trie (Prefix Tree)

    Description: Implement a trie with insert, search, and startsWith methods. Note:You may assume that ...

  8. jQuery的回调管理机制(三)

    jQuery.when()方法是jQuery内部使用回调机制的范例. // 参数为多个方法,这些方法全部执行完成之后执行回调 when: function( subordinate /* , ..., ...

  9. windows中cmd--->进入到别的磁盘

    方法:直接敲:  f:     不要加cd,在同一个磁盘的盘符下用cd.

  10. Jquery操作select选项集合,判断集合中是否存在option

    转载:http://www.cnblogs.com/pepcod/archive/2012/07/03/JavaScript.html Query获取Select选择的Text和Value: 语法解释 ...