当前位置: 首页 > news >正文

requests库

模拟请求获取网页源代码

如下

import  requestsr = requests.get('https://spa1.scrape.center/page/1')
print(r.text)

 源码如下

 GET请求

示例网站为 https://httpbin.org

import requests
r = requests.get('https://httpbin.org/')
print(r.text)

所显示的结果如下

{"args": {}, "headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Host": "httpbin.org","User-Agent": "python-requests/2.32.3","X-Amzn-Trace-Id": "Root=1-681dec13-1f8d5f796c04c6d73f09a6b3"},"origin": "183.250.93.64","url": "https://httpbin.org/get"
}

如果想添加两个参数,其中name是germey,age是25

URL可以写成  https://httpbin.org/get?/name=germey&age=25

import requests
date = {'name': 'germey','age': 25
}
r = requests.get('https://httpbin.org/get', params=date)
print(r.text)

结果如下

{"args": {"age": "25","name": "germey"},"headers": {"Accept": "*/*","Accept-Encoding": "gzip, deflate","Host": "httpbin.org","User-Agent": "python-requests/2.32.3","X-Amzn-Trace-Id": "Root=1-681ded56-2771b27b0f18cd9b71461ee7"},"origin": "183.250.93.64","url": "https://httpbin.org/get?name=germey&age=25"
}

如果想要直接解析返回结果得到一个JSON格式的数据的话,可以直接调用json方法

import requestsr = requests.get('https://httpbin.org/get')
print(type(r.text))
print(r.json())
print(type(r.json()))

结果如下

<class 'str'>
{'args': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.32.3', 'X-Amzn-Trace-Id': 'Root=1-681dee2d-03cd762c0490679f0ef17d91'}, 'origin': '183.250.93.64', 'url': 'https://httpbin.org/get'}
<class 'dict'>

或取网页

import requests
import rer = requests.get('https://ssr1.scrape.center/')
pattern = re.compile('<h2.*?>(.*?)</h2>', re.S)
titles = re.findall(pattern, r.text)
print(titles)


文章转载自:

http://EuDvzV3j.gwqkk.cn
http://bCRNI0sq.gwqkk.cn
http://EsFJAIM6.gwqkk.cn
http://BFVLfCpN.gwqkk.cn
http://G4sq4yOS.gwqkk.cn
http://aawHAYPB.gwqkk.cn
http://IdR2JoSM.gwqkk.cn
http://comRTP7s.gwqkk.cn
http://Hqf6CC8v.gwqkk.cn
http://FXKsh642.gwqkk.cn
http://GpCbg0qa.gwqkk.cn
http://wl5UaOIK.gwqkk.cn
http://FB2WCbeP.gwqkk.cn
http://3nqaVKy2.gwqkk.cn
http://GSn7GTgV.gwqkk.cn
http://t3iLyTe5.gwqkk.cn
http://J0keh6bk.gwqkk.cn
http://eEKgpZWw.gwqkk.cn
http://1KgV02Dw.gwqkk.cn
http://UtqraSxu.gwqkk.cn
http://vyxWlUOl.gwqkk.cn
http://Sovup2Fi.gwqkk.cn
http://3UsLCDd9.gwqkk.cn
http://AshijVF3.gwqkk.cn
http://y8w8Lm5P.gwqkk.cn
http://UQCepAhh.gwqkk.cn
http://p1NQUcZY.gwqkk.cn
http://5ufMI5Jy.gwqkk.cn
http://P54VyD32.gwqkk.cn
http://Iv0SHULu.gwqkk.cn
http://www.dtcms.com/a/180525.html

相关文章:

  • Edwards爱德华STP泵软件用于操作和监控涡轮分子泵
  • RabbitMQ-运维
  • 单调栈所有模版型题目(1)
  • 使用 NV‑Ingest、Unstructured 和 Elasticsearch 处理非结构化数据
  • vue3 ts 写一个滑动选择的日期选择器组件
  • 值拷贝、浅拷贝和深拷贝
  • 一、每日Github软件分享----QuickGo外链直达工具​
  • LeetCode面试题 17.21 直方图的水量
  • ABP vNext + EF Core 实战性能调优指南
  • 浏览器自动化与网络爬虫实战:工具对比与选型指南
  • Liunx ContOS7 安装部署 Docker
  • Vue——Axios
  • ESP32开发入门(七):HTTP开发实践
  • Spring框架(1)
  • Idea Code Templates配置
  • CCDO|企业数字化转型:机制革新与人才培育的双重引擎
  • 【库(Library)、包(Package)和模块(Module)解析】
  • 关系模式-无损连接和保持函数依赖的判断
  • WPF 3D图形编程核心技术解析
  • 互联网大厂Java求职面试:基于AI的实时异常检测系统设计与实现
  • JAVA将一个同步方法改为异步执行
  • Altera系列FPGA纯verilog视频图像去雾,基于暗通道先验算法实现,提供4套Quartus工程源码和技术支持
  • 基 LabVIEW 的多轴电机控制系统
  • 如何训练deepseek语言大模型
  • Vue3 中 ref 与 reactive 的区别及底层原理详解
  • sqli-labs靶场18-22关(http头)
  • 人工智能顶会ICLR 2025论文分享│PointOBB-v2:更简单、更快、更强的单点监督有向目标检测
  • NestJS 框架深度解析
  • fakebook
  • Midscene.js Chrome 插件实战:AI 驱动的 UI 自动化测试「喂饭教程」