Python—requests的使用及案例

简介: Python—requests的使用及案例

1、基本使用

1.1、安装

pip install requests -i https://pypi.douban.com/simple

1.2、一个类型和六个方法

一个类型: Response

六个属性:

response.encoding = ‘utf-8’      设置网页编码格式

response.text          以字符串形式返回网页源码

response.url          获取请求的url

response.content           返回二进制数据

response.status_code      返回响应的状态码

response.headers           返回响应头


2、get请求

应用实例

import requests
url = 'https://www.baidu.com/s?'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
data = {
    'wd':'北京'
}
response = requests.get(url=url,params=data,headers=headers)
content = response.text
print(content)

3、post请求

应用实例

import json
import requests
url = 'https://fanyi.baidu.com/sug'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
data = {
    'kw':'eye'
}
response = requests.post(url=url,data=data,headers=headers)
content = response.text
obj = json.loads(content)
print(obj)

4、代理

应用实例

import requests
url = 'https://www.baidu.com/s?'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
data = {
    'wd':'ip'
}
proxy = {
    'http':'183.246.170.14:30001'
}
response = requests.get(url=url,params=data,headers=headers,proxies=proxy)
content = response.text
with open("daili.html","w",encoding="utf-8") as fp:
    fp.write(content)

5、cookie登录古诗文网

import requests
from lxml import etree
from bs4 import BeautifulSoup
# 登录页面的地址
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
response  = requests.get(url=url,headers=headers)
content = response.text
tree = etree.HTML(content)
# (1) 获取 __VIEWSTATE
viewstate = tree.xpath('//input[@id="__VIEWSTATE"]/@value')
# (2) 获取 __VIEWSTATEGENERATOR
viewstategenerntor = tree.xpath('//input[@id="__VIEWSTATEGENERATOR"]/@value')
# (3) 获取验证码图片
code = tree.xpath('//img[@id="imgCode"]/@src')[0]
code_url = 'https://so.gushiwen.cn' + code
# 下载验证码图片到本地,然后输入
session = requests.session()
# 验证码的url内容
response_code = session.get(code_url)
# 注意此时要使用二进制,因为我们要使用图片的下载
content_code = response_code.content
with open("code.jpg", "wb") as fp:
    fp.write(content_code)
code_name = input('请输入你的验证码:')
# 点击登录
url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data_post = {
    '__VIEWSTATE':viewstate,
    '__VIEWSTATEGENERATOR':viewstategenerntor,
    'from': 'http://so.gushiwen.cn/user/collect.aspx',
    'email': '15284124517',
    'pwd': 'xy251753',
    'code': code_name,
    'denglu': '登录'
}
response_post = session.post(url=url_post,headers=headers,data=data_post)
content_post = response_post.text
with open("gushiwen.html", "w", encoding='utf-8') as fp:
    fp.write(content_post)






相关文章
|
3天前
|
数据采集 存储 API
Python 网络请求:深入理解Requests库
Python 网络请求:深入理解Requests库
60 0
|
1天前
|
Python
【python学习小案例】提升兴趣之模拟系统入侵,2024年最新面试阿里运营一般问什么
【python学习小案例】提升兴趣之模拟系统入侵,2024年最新面试阿里运营一般问什么
|
3天前
|
Python
Python自动化办公实战案例:文件整理与邮件发送
Python自动化办公实战案例:文件整理与邮件发送
8 0
|
3天前
|
存储 数据挖掘 数据处理
使用Python将数据表中的浮点数据转换为整数:详细教程与案例分析
使用Python将数据表中的浮点数据转换为整数:详细教程与案例分析
7 2
|
3天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
【5月更文挑战第9天】`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
30 2
|
3天前
|
JSON 数据格式 开发者
pip和requests在Python编程中各自扮演着不同的角色
【5月更文挑战第9天】`pip`是Python的包管理器,用于安装、升级和管理PyPI上的包;`requests`是一个HTTP库,简化了HTTP通信,支持各种HTTP请求类型及数据交互。两者在Python环境中分别负责包管理和网络请求。
32 5
|
3天前
|
数据采集 JSON API
如何用Python Requests发送请求
如何用Python Requests发送请求
12 0
|
3天前
|
数据采集 Web App开发 Java
Python 爬虫:Spring Boot 反爬虫的成功案例
Python 爬虫:Spring Boot 反爬虫的成功案例
|
3天前
|
机器学习/深度学习 数据采集 数据可视化
利用Python进行历史数据预测:从入门到实践的两个案例分析
利用Python进行历史数据预测:从入门到实践的两个案例分析
25 1
|
3天前
|
前端开发 JavaScript Python
使用Python读取本地行情csv文件,做出web网页画出K线图实现案例
【5月更文挑战第4天】使用Python绘制K线图的步骤:1) 安装pandas, matplotlib和Flask;2) 用pandas读取CSV文件并处理数据;3) 创建Flask应用,渲染包含K线图数据的HTML;4) 编写HTML,使用ECharts库绘制K线图。
27 0