requests库的get请求(加上head,加上get参数请求)
#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参数请求)的更多相关文章
- 【python接口自动化】- 使用requests库发送http请求
前言:什么是Requests ?Requests 是⽤Python语⾔编写,基于urllib,采⽤Apache2 Licensed开源协议的 HTTP 库.它⽐ urllib 更加⽅便,可以节约我们⼤ ...
- 使用Python的requests库进行接口测试——session对象的妙用
from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...
- requests库写接口测试框架初学习
学习网址: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-dscpm/ff75b907-415d-4220-89 ...
- [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍
[根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...
- Python爬虫:HTTP协议、Requests库
HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...
- 【Python爬虫】Requests库的基本使用
Requests库的基本使用 阅读目录 基本的GET请求 带参数的GET请求 解析Json 获取二进制数据 添加headers 基本的POST请求 response属性 文件上传 获取cookie 会 ...
- Python Requests库简单入门
我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...
- 爬虫1.1-基础知识+requests库
目录 爬虫-基础知识+requests库 1. 状态返回码 2. URL各个字段解释 2. requests库 3. requests库爬虫的基本流程 爬虫-基础知识+requests库 关于html ...
- Python爬虫:HTTP协议、Requests库(爬虫学习第一天)
HTTP协议: HTTP(Hypertext Transfer Protocol):即超文本传输协议.URL是通过HTTP协议存取资源的Internet路径,一个URL对应一个数据资源. HTTP协议 ...
- requests库的使用、安装及方法的简单介绍
requests库的使用.安装及方法的简单介绍 1.requests库的概述 requests库是一个简洁且简单的处理HTTP请求的第三方库,是公认的最好获得第三方信息的库. requests库更多信 ...
随机推荐
- cannot access android.support.v4.app.BaseFragmentActivityJB的解决
//implementation 'com.android.support:appcompat-v7:26.1.0' 改成implementation 'com.android.support:app ...
- AngularJs HTML DOM、AngularJS 事件以及模块的学习(5)
今天的基础就到了操作DOM,事件和模块的学习,其实我个人感觉学习起来AngularJS并没有想象中的那么的艰难,可能是因为这个太基础化吧,但是我们从初学开始就应该更加的自信一些,后来我可能会写一个小的 ...
- UML类图的几种关系总结【转】
在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合(Composit ...
- leetCode练习题
1.求二叉树的最小深度: public class Solution { public int run(TreeNode root) { if(root==null) return 0; int l ...
- HipHop的原理
HipHop的原理 HipHop将PHP代码转换为高度优化的C++代码,然后再用g++编译器编译.它可以保持语义等效地执行源代码,但为了提高性能,牺牲了一些很少用到的特性,比如eval(). HipH ...
- 常用的数据整理的JavaScript库
Lodash.js https://lodash.com/ Underscore.js https://www.html.cn/doc/underscore/
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- 时间的类型的相互转换(date/String)及时区的比较
String ->Date ->String @Test public void date() throws ParseException{ String sdate = "01 ...
- Cordova 3.3 开发环境搭建(视频)
图文文章参见: http://www.cnblogs.com/mlzs/p/3332199.html 视频共享链接 百度:http://pan.baidu.com/s/1c0EHfqC
- (TOJ 4413)IP address
描述 To give you an IP address, it may be dotted decimal IP address, it may be 32-bit binary IP addres ...