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. 逐步构建循环神经网络 RNN

    rnn.utils.py import numpy as np def softmax(x): e_x = np.exp(x - np.max(x)) return e_x / e_x.sum(axi ...

  2. 由装饰者模式来深入理解Java I/O整体框架

    前言 Java里面的I/O这一部分看过很多遍,每次看完之后特别混乱,又是输入流,又是输出流,又是字符流,又是字节流,还有什么过滤流,缓冲流.每次看得我如入云里雾里,直到后面看了设计模式这一块,才算真正 ...

  3. SQL Server数据归档的解决方案

    SQL Server数据归档的解决方案   最近新接到的一项工作是把SQL Server中保存了四五年的陈年数据(合同,付款,报销等等单据)进行归档,原因是每天的数据增量很大,而历史数据又不经常使用, ...

  4. LeetCode--017--电话号码的字母组合(java)

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  5. 关于Djanggo的环境变量

    templates是Django指定的T目录,pycharm下templates的图标会变亮,static则可以改动.下面STATIC_URL可以指定,STATICFILES_DIRS则指定了temp ...

  6. FTP:500 OOPS: failed to open vsftpd log file:/var/log/vsftpd.log

    如下:从10.12.8.165 FTP 到 10.1.3.34,报failed to open vsftpd log[a4_csbdc@localhost ~]$ ftp  10.1.3.34Conn ...

  7. 单点登录系统实现基于SpringBoot

    今天的干货有点湿,里面夹杂着我的泪水.可能也只有代码才能让我暂时的平静.通过本章内容你将学到单点登录系统和传统登录系统的区别,单点登录系统设计思路,Spring4 Java配置方式整合HttpClie ...

  8. 如何seo(搜索引擎优化)

    Seo是指遵循搜索引擎的搜索原则,对网站结构.网页文字语言和站点间互动外交等进行合理规划部署,以改善网站在搜索引擎的搜索表现,从而增加客户发现并访问网站的可能性的一个过程.

  9. Java toBinaryString()函数探究及Math.abs(-2147483648)=-2147483648原理探究

    toBinaryString()函数 public class Customer { public static void main(String[] args) { int m=-8; System ...

  10. python url 下载并保存

    方法一:用urllib import jsonimport urllib url_model='{"version":"2","url":& ...