1.发送get请求

import requests

# response=requests.get('http://www.baidu.com')
# 查看响应内容,返回的是已经解码的内容
# response.text 服务器返回的数据,已解码。解码类型:根据HTTP头部对响应的编码做出有根据的推测,推测的文本编码
# print(type(response.text))
# print(response.text)
# 百度返回的text有乱码,说明解码猜测的编码方式不对 # 查看响应内容
# print(type(response.content))
# print(response.content.decode('utf-8'))
# 解码正确,没有乱码 # 查看完整url地址
# print(response.url) # 查看响应头部字符编码
# print(response.encoding) # 查看响应码
# print(response.status_code) params = {'wd': '中国'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
#params传入,会自动进行编码
response=requests.get('http://www.baidu.com/s',headers=headers,params=params)
print(response.url)
with open('baidu.html','w',encoding='utf-8') as f:
f.write(response.content.decode('utf-8'))

2.发送post请求

import requests

data = {
'first': True, 'pn': 1, 'kd': 'python'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36',
'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
'Cookie':'JSESSIONID=ABAAABAAAFCAAEGE19E4DE9949656042D040782B344E314; SEARCH_ID=94069a753d8a4157a4b8a44284d4b719; user_trace_token=20190404002147-ba37c7c8-aa84-4a31-8171-738e6bcfadf2; X_HTTP_TOKEN=42daf4b72327b2817058034551bf5e71415983ed09'
} response = requests.post('https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false',headers=headers,data=data)
print(response.text)
#把传来的数据转成原本数据类型(如果传输的是json格式的字符串)
# print(response.json()) #测试
# ret=response.text
# # import json
# # ret=json.loads(ret)
# # print(type(ret))

3.使用代理

import requests

#不使用代理
# response=requests.get('http://httpbin.org/ip')
# print(response.text) #使用代理
#尽量使用高匿名的代理,透明的话,它依然能识别原来的ip地址。
proxy={'http':'112.85.149.79:9999'}
response=requests.get('http://httpbin.org/ip',proxies=proxy)
print(response.text)

4.处理cookie信息

import requests

# response=requests.get('http://www.baidu.com')
#返回的是一个对象
# print(response.cookies)
#获取字典形式信息
# print(response.cookies.get_dict()) #session
#之前使用的urlib库,是可以使用opener发送多个请求,多个请求之间是可以共享cookie的。那么如果使用requests,
#也要达到共享cookie的目的,那么可以使用requests库提供的session对象。它简化了我们每次模拟请求时都要带上cookie
#的复杂操作,使用session它自己会帮我们带上headers里面的cookie信息
url='http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019341135380'
session=requests.session()
data={
'email':'9@qq.com',
'password':'pythonspr'
}
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
session.post(url,data=data,headers=headers)
#只有登录后才能查看大鹏的页面
response=session.get('http://www.renren.com/880151247/profile',headers=headers)
with open('renren.html','w',encoding='utf-8') as f:
f.write(response.text)
#查看页面,确实登录成功

5.处理不信任的ssl证书

#处理不信任的ssl证书,加上verify=False就可以了
import requests
resp=requests.get('http://www.12306.cn',verify=False)
print(resp.text)

最新文章

  1. 《疯狂Java讲义》学习笔记——第2章 理解面向对象
  2. Atitit. 查找linux 项目源码位置
  3. git学习(二):查看状态和具体改动
  4. php +ajax
  5. libcurl 函数curl_easy_perform在release下崩溃的问题
  6. node-webkit 写桌面系统
  7. caffe: fuck compile error again : error: a value of type "const float *" cannot be used to initialize an entity of type "float *"
  8. xv6实验环境搭建
  9. Git版本管理:Windows下Git配置与使用指南 Gitlab
  10. j2ee基础
  11. Spring4 MVC Hibernate4 maven集成
  12. mysql GTID主从配置
  13. Sidetiq 定时任务
  14. Typescript 常见的几种函数重载方法详解与应用示例
  15. 一款纯css3实现的颜色渐变按钮
  16. json 转 T
  17. SQL索引器
  18. reactnative 原生组件通信原理
  19. unity3d-地图制作之云彩飘动
  20. shiro笔记-"Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - ylw, rememberMe=false]. Possible unexpected error? (Typical or expected login exceptions should ext

热门文章

  1. Accumulation Degree
  2. 一 .isinstance(obj,cls)和issubclass(sub,super)
  3. Reveal.js一个用来做WEB演示文稿的框架
  4. [转自大神]js拖拽小总结
  5. python基础篇_006_面向对象
  6. leetcode 91 Decode Ways I
  7. Chapter 4 : Control Structures 1 : Selection
  8. angular.js使用ui-router注入报错,这里是版本问题导致的
  9. __x__(4)0905第二天__软件架构
  10. Node.js_Buffer 缓冲区