requests模块

  • 什么是request模块:requests是python原生一个基于网络请求的模块,模拟浏览器发起请求。

requests-get请求

# get请求
import requests
# 指定url
url = 'https://www.sogou.com/' # 发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url)
if response.status_code == 200:
with open('sougo.html','w') as f:
f.write(response.text)
else:
print('页面获取失败')

response常用属性

# get请求
import requests
# 指定url
url = 'https://www.sogou.com/' # 发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url)
if response.status_code == 200:
# print(response.text) # 文本
print(response.status_code) # 返回一个响应状态码
print(response.content) # content获取的是response对象中二进制(byte)类型的页面数据
print(response.headers) # 获取响应头信息
print(response.url) # 获取请求的url
else:
print('页面获取失败')

携带参数的get请求

  • 方式1
import requests
# 指定url,参数不需要进行编码处理
url = 'https://www.sogou.com/web?query=周杰伦&ie=utf-8' # 发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url)
if response.status_code == 200:
with open('jay.html','wb') as f:
f.write(response.content)
else:
print('页面获取失败')
  • 方式2
import requests
url = 'https://www.sogou.com/web' params = {
'query':'周杰伦',
'ie':'utf-8'
}
response = requests.get(url,params=params)
if response.status_code == 200:
with open('jay.html','wb') as f:
f.write(response.content)
else:
print('页面获取失败')

get请求自定义请求头信息

# 自定义请求头信息
import requests
url = 'https://www.sogou.com/web'
# 自定义的请求头信息放在该字典中,然后发请求的时候传到headers参数中
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
params = {
'query':'林宥嘉',
'ie':'utf-8'
}
response = requests.get(url=url,params=params,headers=headers)
print(response.status_code)

requests-post请求

# post请求

# 指定url
url = 'https://github.com/session'
data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': 'IRdX8jflo9hKJAZ9mOzQBNnVnOFD7z9MfKvSYCOvrVN4uWz/LDQ81b6wWWy4d8YrvYobfiuLYS92zoK6XgH/LQ==',
'login': '1032298871@qq.com',
'password': '09212427zlh'
}
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
response = requests.post(url=url,data=data,headers=headers)
with open('github.html','w',encoding='utf-8') as f:
f.write(response.text)

requests模块ajax的get请求

# 基于ajax的get请求
import requests
url = 'https://movie.douban.com/j/new_search_subjects?'
data = {
'sort': 'U',
'range':'0,10',
'tags': '电影',
'start': '40'
}
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
response = requests.get(url=url,data=data,headers=headers)
# ajax返回的数据类型是json字符串类型
print(response.text)

requests模块ajax的post请求

# 基于ajax的post请求
import requests
import json
url = 'https://fanyi.baidu.com/sug'
data = {
'kw': '西瓜'
}
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
response = requests.post(url=url,headers=headers,data=data)
json_text =response.text
json_data = json.loads(json_text)
print(json_data)

爬取多页数据

# 爬取带有分页的数据
import requests
import os if not os.path.exists('./page'):
os.mkdir('page') url = 'https://zhihu.sogou.com/zhihu?'
work= input('想搜索什么内容')
page_number = input('想获取前几页的内容')
for page in range(1,int(page_number)+1):
print(page)
params = {
'query': work,
'sut': '13598',
'lkt': '1,1546144033954,1546144033954',
'sst0': '1546144034930',
'page': page,
'ie': 'utf8'
}
response = requests.get(url=url,params=params)
page_text = response.text
page_file = './page/%s%s.html'%(work,page)
with open(page_file,'w',encoding='utf-8') as f:
f.write(page_text)

requests模块高级:

cookie作用:服务器端使用cookie来记录客户端的状态信息

import requests

session = requests.session()
#1.发起登录请求:将cookie获取,切存储到session对象中
login_url = 'https://accounts.douban.com/login'
data = {
"source": "None",
"redir": "https://www.douban.com/people/185687620/",
"form_email": "15027900535",
"form_password": "bobo@15027900535",
"login": "登录",
}
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
}
#使用session发起post请求
login_response = session.post(url=login_url,data=data,headers=headers) #2.对个人主页发起请求(session(cookie)),获取响应页面数据
url = 'https://www.douban.com/people/185687620/'
response = session.get(url=url,headers=headers)
page_text = response.text with open('./douban110.html','w',encoding='utf-8') as fp:
fp.write(page_text)

requests使用ip代理

# 使用代理ip爬取百度搜索ip
import requests url = 'http://www.baidu.com/s?ie=UTF-8&wd=ip' # 传入的代理ip是个字典,key是协议,value是ip:端口
proxy = {
'http':'115.28.209.249:3128'
}
response = requests.get(url=url,proxies=proxy)
with open('daili.html','w') as f:
f.write(response.text)

requests模块的使用的更多相关文章

  1. 爬虫requests模块 1

    让我们从一些简单的示例开始吧. 发送请求¶ 使用 Requests 发送网络请求非常简单. 一开始要导入 Requests 模块: >>> import requests 然后,尝试 ...

  2. requests 模块

    发送请求 使用Requests发送网络请求非常简单. 一开始要导入Requests模块: >>> import requests 然后,尝试获取某个网页.本例子中,我们来获取Gith ...

  3. requests模块--python发送http请求

    requests模块 在Python内置模块(urllib.urllib2.httplib)的基础上进行了高度的封装,从而使得Pythoner更好的进行http请求,使用Requests可以轻而易举的 ...

  4. Python requests模块学习笔记

    目录 Requests模块说明 Requests模块安装 Requests模块简单入门 Requests示例 参考文档   1.Requests模块说明 Requests 是使用 Apache2 Li ...

  5. Python高手之路【八】python基础之requests模块

    1.Requests模块说明 Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2  ...

  6. Python requests模块

    import requests 下面就可以使用神奇的requests模块了! 1.向网页发送数据 >>> payload = {'key1': 'value1', 'key2': [ ...

  7. 基于python第三方requests 模块的HTTP请求类

    使用requests模块构造的下载器,首先安装第三方库requests pip install requests 1 class StrongDownload(object): def __init_ ...

  8. 使用requests模块爬虫

    虽然干技术多年了,但从没有写过博客,想来甚是惭愧,本篇作为我博客的第一篇,也是测试篇.不为写的好,只为博诸君一眸而已. 使用python爬虫,有几个比较常用的,获取html_content的模块url ...

  9. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  10. python爬虫之requests模块介绍

    介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内容下 ...

随机推荐

  1. 绑定到外部验证服务LDAP、配置 autofs

    题1:您的系统需要按照以下要求绑定到这个服务上:验证服务器的基本 DN 是: dc=xxxx,dc=xxxx,dc=xxxx. 帐户信息和验证信息都是由 LDAP 提供的.连 接 需 要 使 用 证 ...

  2. nrm 安装与使用

    1.使用 npm install nrm -global 全局安装 2.安装完成后使用 nrm ls命令查看其维护的镜像地址列表 3.* 星号表示在使用 npm下载资源的时候,默认使用的地址 这里需要 ...

  3. vivado 创建PL工程

    参考来源 https://china.xilinx.com/video/hardware/i-and-o-planning-overview.html 前言 我Win10系统上的Xilinx Plat ...

  4. 四:DRF项目开发的准备

    一: 虚拟环境virtualenv 如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就 ...

  5. get函数

    dict={"name":"jary","age":22}print(dict.get("age")) # 通过键值找到 ...

  6. Python 运行uiKLine.py ,PyQt4错误

    python 开发环境tool: 在运行项目中出现 NO module name PyQt4 错误 解决:

  7. localStorage(本地存储器)、sessionStorage(会话存储)

      设置:localStorage.setItem("token", JSON.parse(res).data.token);   获取:that.token = localSto ...

  8. Django框架简介-视图系统

    2.3 视图系统 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档 ...

  9. 关于vs code 快速生成vue 模板

    在 文件>首选项>用户代码片断里面,打开vue.json 添加以下代码: "Print to console": { "prefix": " ...

  10. React文档(十三)思考React

    在我们的看来,React是使用js创建大型快速网站应用的首要方法.它在Facebook和Instagram的使用已经为我们展现了它自己. React的一个很好的地方就在于当你创建应用的时候它使你思考如 ...