python爬虫入门(1)----- requests
介绍
requests是python实现的简单易用的HTTP库,使用起来比urllib简洁很多
基本使用
requests.get("http://www.baidu.com")
requests.post("http://www.baidu.com")
requests.put("http://www.baidu.com")
requests.delete("http://www.baidu.com")
requests.request("get", "http://www.baidu.com")
get
def get(url, params=None, **kwargs):
r"""Sends a GET request. :param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
""" kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
下面凡科微传单获取模板的接口为例子
import requests
param = {
"cmd": "getTemplate",
"scrollIndex": 0
}
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}//通过ua识别是否是爬虫
rep = requests.get("https://cd.fkw.com/ajax/flyerhome.jsp", params=param, headers=header)
rep.encoding = 'utf8'
print(rep.text)
post
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request. :param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
""" return request('post', url, data=data, json=json, **kwargs)
一样以凡科微传单接口为例
import requests
data = {
"cmd": "getTemplate",
"scrollIndex": 0
}
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}
rep = requests.post("https://cd.fkw.com/ajax/flyerhome.jsp", data=data, headers=header)
rep.encoding = 'utf8'
print(rep.text)
会话对象
在上面操作中request不会持有cookie对象导致每次请求都是新的会话,requests库提供了session的解决方案,下面以凡科登录和登录状态下获取模板为例
import requests
import _md5
import json
import re s = requests.session() md5 = _md5.md5()
md5.update("pwd".encode("utf8"))
pwd = md5.hexdigest()
data = {
"cmd": "loginCorpNew",
"cacct": "username",
"pwd": pwd
}
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
}
rep = s.post("https://i.fkw.com/ajax/login_h.jsp?dogSrc=3", data=data, headers=header)
login = json.loads(rep.text)
tokenStr = login.get("_TOKEN")
print(tokenStr)
pattern = "value='(.+)'"
matcher = re.search(pattern, rep.text)
if matcher:
token = matcher.group(1)
print(token)
param = {
"cmd": "getTemplate",
"_TOKEN": token,
"scrollIndex": 0
}
rep = s.get("https://i.cd.fkw.com/ajax/flyerTemplate_h.jsp", params=param, headers=header)
print(rep.text)
参考文献
https://cuiqingcai.com/2556.html
http://docs.python-requests.org/en/master/api/
python爬虫入门(1)----- requests的更多相关文章
- Python 爬虫入门(requests)
相信最开始接触Python爬虫学习的同学最初大多使用的是urllib,urllib2.在那之后接触到了第三方库requests,requests完全能满足各种http功能,真的是好用爆了 :D 他们是 ...
- Python爬虫入门——使用requests爬取python岗位招聘数据
爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...
- Python爬虫入门(二)之Requests库
Python爬虫入门(二)之Requests库 我是照着小白教程做的,所以该篇是更小白教程hhhhhhhh 一.Requests库的简介 Requests 唯一的一个非转基因的 Python HTTP ...
- python爬虫入门-开发环境与小例子
python爬虫入门 开发环境 ubuntu 16.04 sublime pycharm requests库 requests库安装: sudo pip install requests 第一个例子 ...
- Python 爬虫入门(二)——爬取妹子图
Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...
- 1.Python爬虫入门一之综述
要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...
- Python 爬虫入门之爬取妹子图
Python 爬虫入门之爬取妹子图 来源:李英杰 链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...
- Python爬虫入门一之综述
大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...
- Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分
1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...
- Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分
1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...
随机推荐
- mysql8.0 解决时区问题
jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8&useOldAlias ...
- postman使用小结(一)
postman可以用来做接口测试. 下面是使用的基本步骤: 1新建http请求: 2设置请求类型get/post/put/delete...: 3设置请求的url: 4设置请求的Header头部信息, ...
- docker推送镜像到私有仓库
配置私有仓库源 私有仓库地址:registry.supos.ai 修改/etc/docker/daemon.json文件,增加insecure-registries,如下所示: { "ins ...
- 痞子衡嵌入式:kFlashFile v1.0 - 一个基于Flash的掉电数据存取方案
大家好,我是痞子衡,是正经搞技术的痞子.今天给大家带来的是痞子衡的个人小项目 - kFlashFile. 痞子衡最近在参与一个基于 i.MXRT1170 的项目,项目有个需求,需要在 Flash 里实 ...
- JDK8 日期格式化
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
- python之shutil模块的使用
shutil模块 shutil模块是一种高级的文件操作工具,其对文件的复制与删除操作非常强大,shutil 名字来源于 shell utilities,该模块拥有许多文件(夹)操作的功能,包括复制.移 ...
- Python必须知道的异常处理
异常处理 把可能会发生的错误,提前在代码里进行捕捉(监测) try : code except Exception: 出错后要执行的代码 下面是常见的异常: attributeError 试图访问一个 ...
- 进阿里真的这么难?P8大佬告诉你,你和阿里之间缺的只是这份笔记
一转眼今年已经到六月份了,在这个过去的半年里有人选择了安稳,有的人偏偏不... 最近小编就有个朋友,去面了个[P8 级架构师],也算是摸摸行情,为后面的一些安排提前做好规划~ 先给大家介绍一下我这个朋 ...
- Apache Hudi重磅特性解读之全局索引
1. 摘要 Hudi表允许多种类型操作,包括非常常用的upsert,当然为支持upsert,Hudi依赖索引机制来定位记录在哪些文件中. 当前,Hudi支持分区和非分区的数据集.分区数据集是将一组文件 ...
- request.headers.get头部获取内容的缺失
1.说明 今天遇到了一个小坑,在做权限控制的时候,用头部传递了参数取名为table_privilege_id, 在本地测试的时候是可以接到这个参数的,但是通过ngxin转发之后,奇怪了,怎么也拿不到这 ...