广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python爬虫之Requests库的基
  • 714
分享到

Python爬虫之Requests库的基

爬虫PythonRequests 2023-01-30 22:01:47 714人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

1 import requests 2 response = requests.get('Http://www.baidu.com/') 3 print(type(response)) 4 print(response.

  1 import requests
  2 response = requests.get('Http://www.baidu.com/')
  3 print(type(response))
  4 print(response.status_code)
  5 print(type(response.text))
  6 print(response.text)
  7 print(response.cookies)
  8 
  9 # 各种请求方式
 10 import requests
 11 requests.post('http://httpbin.org/post')
 12 requests.put('http://httpbin.org/put')
 13 requests.delete('http://httpbin.org/delete')
 14 requests.head('http://httpbin.org/get')
 15 requests.options('http://httpbin.org/get')
 16 
 17 # 基本GET请求
 18 import requests
 19 response = requests.get('http://httpbin.org/get')
 20 print(response.text)
 21 
 22 # 带参数GET请求
 23 import requests
 24 response = requests.get('http://httpbin.org/get?name=germey&age=22')
 25 print(response.text)
 26 
 27 import requests
 28 data = {
 29     'name': 'germey',
 30     'age': 22
 31 }
 32 response = requests.get('http://httpbin.org/get', params = data)
 33 print(response.text)
 34 
 35 # 解析JSON
 36 import requests
 37 import json
 38 response = requests.get('http://httpbin.org/get')
 39 print(type(response.text))
 40 print(response.json())
 41 print(json.loads(response.text))
 42 print(type(response.json()))
 43 
 44 # 获取二进制数据
 45 import requests
 46 response = requests.get('http://GitHub.com/favicon.ico')
 47 print(type(response.text), type(response.content))
 48 print(response.text)
 49 print(response.content)
 50 
 51 # 保存图片
 52 import requests
 53 response = requests.get('http://github.com/favicon.ico')
 54 with open('1.ico', 'wb') as f:
 55     f.write(response.content)
 56     f.close()
 57 
 58 # 添加headers 不添加的话会请求失败的
 59 import requests
 60 response = requests.get('http://www.zhihu.com/explore')
 61 print(response.text)
 62 
 63 import requests
 64 headers = {
 65     'User-Agent': 'Mozilla/5.0 (windows NT 10.0; Win64; x64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
 66 }
 67 response = requests.get('http://zhihu.com/explore', headers = headers)
 68 print(response.text)
 69 
 70 # 基本的POST请求
 71 import requests
 72 data = {'name': 'germey', 'age': 22}
 73 response = requests.post('http://httpbin.org/post', data = data)
 74 print(response.text)
 75 
 76 import requests
 77 data = {'name':'germey', 'age':22}
 78 headers = {
 79     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
 80 }
 81 response = requests.post('http://httpbin.org/post', data = data, headers = headers)
 82 print(response.json())
 83 
 84 # response属性
 85 import requests
 86 response = requests.get('http://www.jianshu.com')
 87 print(type(response.status_code), response.status_code)
 88 print(type(response.headers), response.headers)
 89 print(type(response.cookies), response.cookies)
 90 print(type(response.url), response.url)
 91 print(type(response.history), response.history)
 92 
 93 # 文件上传
 94 import requests
 95 files = {'file':open('1.ico', 'rb')}
 96 response = requests.post('http://httpbin.org/post', files = files)
 97 print(response.text)
 98 
 99 # 获取cookie
100 import requests
101 response = requests.get('http://www.baidu.com')
102 print(response.cookies)
103 for key, value in response.cookies.items():
104     print(key + ' = ' + value)
105 
106 # 会话维持 模拟登陆
107 import requests
108 requests.get('http://httpbin.org/cookies/set/number/123456789')
109 response = requests.get('http://httpbin.org/cookies')
110 print(response.text)
111 
112 import requests
113 s = requests.session()
114 s.get('http://httpbin.org/cookies/set/number/123456789')
115 response = s.get('http://httpbin.org/cookies')
116 print(response.text)
117 
118 # 证书验证
119 import requests
120 response = requests.get('https://www.12306.cn')
121 print(response.status_code)

 

--结束END--

本文标题: Python爬虫之Requests库的基

本文链接: https://www.lsjlt.com/news/180374.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python爬虫之Requests库的基
    1 import requests 2 response = requests.get('http://www.baidu.com/') 3 print(type(response)) 4 print(response....
    99+
    2023-01-30
    爬虫 Python Requests
  • Python爬虫之requests库基本介绍
    目录一、说明二、基本用法:总结一、说明 requests是一个很实用的Python HTTP客户端库,爬虫和测试服务器响应数据时经常会用到,requests是Python语言的第三方...
    99+
    2022-11-13
  • python爬虫之requests库的使用详解
    目录python爬虫—requests库的用法基本的get请求带参数的GET请求:解析json使用代理获取cookie会话维持证书验证设置超时异常捕获异常处理 总结 python爬虫...
    99+
    2022-11-12
  • python爬虫之requests库使用代理方式
    目录安装上requests库GET方法谷歌浏览器的开发者工具POST方法使用代理在看这篇文章之前,需要大家掌握的知识技能: python基础html基础http状态码 让我们看看这篇...
    99+
    2022-11-11
  • Python爬虫Requests库的使用详情
    目录一、Requests库的7个主要的方法二、Response对象的属性三、爬取网页通用代码四、Resquests库的常见异常五、Robots协议展示六、案例展示一、Requests...
    99+
    2022-11-11
  • Python爬虫Requests库如何使用
    本篇内容主要讲解“Python爬虫Requests库如何使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python爬虫Requests库如何使用”吧!1、安装 requests 库因为学习过...
    99+
    2023-07-06
  • python爬虫中requests库怎么用
    小编给大家分享一下python爬虫中requests库怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!python爬虫—requests库的用法request...
    99+
    2023-06-25
  • Python爬虫之Urllib库的基本使
    # get请求 import urllib.request response = urllib.request.urlopen("http://www.baidu.com") print(response.read().decode('...
    99+
    2023-01-30
    爬虫 Python Urllib
  • python: 爬虫利器requests
    requests并不是系统自带的模块,他是第三方库,需要安装才能使用 闲话少说,来,让我们上代码:简单的看一下效果: import requests requests = requests.session() headers = { ...
    99+
    2023-01-31
    爬虫 利器 python
  • python爬虫之利用Selenium+Requests爬取拉勾网
    目录一、前言二、分析url三、获取所有城市和页数四、生成params参数五、获取数据六、总结一、前言 利用selenium+requests访问页面爬取拉勾网招聘信息 二、分析url...
    99+
    2022-11-12
  • Python之爬虫基础
    网络爬虫(又称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫...
    99+
    2023-08-31
    python 爬虫 开发语言
  • Python爬虫学习之requests的使用教程
    目录requests库简介requests库安装1、pip命令安装2、下载代码进行安装requests库的使用发送请求get请求抓取二进制数据post请求POST请求的文件上传利用r...
    99+
    2022-11-11
  • 关于Python网络爬虫requests库的介绍
    1. 什么是网络爬虫 简单来说,就是构建一个程序,以自动化的方式从网络上下载、解析和组织数据。 就像我们浏览网页的时候,对于我们感兴趣的内容我们会复制粘贴到自己的笔记本中,方便下次阅...
    99+
    2023-05-18
    Python网络爬虫 Python requests库
  • Python网络爬虫requests库怎么使用
    1. 什么是网络爬虫简单来说,就是构建一个程序,以自动化的方式从网络上下载、解析和组织数据。就像我们浏览网页的时候,对于我们感兴趣的内容我们会复制粘贴到自己的笔记本中,方便下次阅读浏览——网络爬虫帮我...
    99+
    2023-05-15
    Python Requests
  • Python网络爬虫requests库如何使用
    这篇文章主要讲解了“Python网络爬虫requests库如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Python网络爬虫requests库如何使用”吧!1. 什么是网络爬虫简单来...
    99+
    2023-07-06
  • Python爬虫基础之selenium库怎么用
    小编给大家分享一下Python爬虫基础之selenium库怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!一、selenium简介官网总的来说: seleni...
    99+
    2023-06-15
  • 基础爬虫,谁学谁会,用requests、
    爬取豆瓣Top250电影的评分、海报、影评等数据!   本项目是爬虫中最基础的,最简单的一例; 后面会有利用爬虫框架来完成更高级、自动化的爬虫程序。   此项目过程是运用requests请求库来获取html,再用正则表达式来解析从中获取所...
    99+
    2023-01-30
    爬虫 谁会 基础
  • python基础之爬虫入门
    目录前言一、简单静态网页的爬取1.1 选取爬虫策略——缩略图1.2 选取爬虫策略——高清大图二、动态加载网站的爬取2.1 选取爬虫策略——selenium2.2 选取爬虫策略——ap...
    99+
    2022-11-12
  • python爬虫之『入门基础』
    1.首先需要了解一下http请求,当用户在地址栏中输入网址,发送网络请求的过程是什么? 可以参考我之前学习的时候转载的一篇文章一次完整的HTTP事务过程–超详细 2.还需要了解一下http的请求方式 有兴趣的同学可以去查一下http的八...
    99+
    2023-01-31
    爬虫 入门 基础
  • Python爬虫基础之爬虫的分类知识总结
    目录一、通用爬虫二、搜索引擎的局限性三、Robots协议四、请求与相应一、通用爬虫 通用网络爬虫是搜索引擎抓取系统(Baidu、Google、Sogou等)的一个重要组成部分。主要目...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作