介绍

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的更多相关文章

  1. Python 爬虫入门(requests)

    相信最开始接触Python爬虫学习的同学最初大多使用的是urllib,urllib2.在那之后接触到了第三方库requests,requests完全能满足各种http功能,真的是好用爆了 :D 他们是 ...

  2. Python爬虫入门——使用requests爬取python岗位招聘数据

    爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...

  3. Python爬虫入门(二)之Requests库

    Python爬虫入门(二)之Requests库 我是照着小白教程做的,所以该篇是更小白教程hhhhhhhh 一.Requests库的简介 Requests 唯一的一个非转基因的 Python HTTP ...

  4. python爬虫入门-开发环境与小例子

    python爬虫入门 开发环境 ubuntu 16.04 sublime pycharm requests库 requests库安装: sudo pip install requests 第一个例子 ...

  5. Python 爬虫入门(二)——爬取妹子图

    Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob ...

  6. 1.Python爬虫入门一之综述

    要学习Python爬虫,我们要学习的共有以下几点: Python基础知识 Python中urllib和urllib2库的用法 Python正则表达式 Python爬虫框架Scrapy Python爬虫 ...

  7. Python 爬虫入门之爬取妹子图

    Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果 ...

  8. Python爬虫入门一之综述

    大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...

  9. Python爬虫入门教程 48-100 使用mitmdump抓取手机惠农APP-手机APP爬虫部分

    1. 爬取前的分析 mitmdump是mitmproxy的命令行接口,比Fiddler.Charles等工具方便的地方是它可以对接Python脚本. 有了它我们可以不用手动截获和分析HTTP请求和响应 ...

  10. Python爬虫入门教程 43-100 百思不得姐APP数据-手机APP爬虫部分

    1. Python爬虫入门教程 爬取背景 2019年1月10日深夜,打开了百思不得姐APP,想了一下是否可以爬呢?不自觉的安装到了夜神模拟器里面.这个APP还是比较有名和有意思的. 下面是百思不得姐的 ...

随机推荐

  1. 输入url后浏览器干了些什么(详解)

    输入url后浏览器干了些什么(详解) DNS(Domain Name System, 域名系统) 解析 DNS解析的过程就是寻找哪台机器上有你真正需要的资源过程.但你在浏览器张红输入一个地址时,例如: ...

  2. 06[笔记] SpringBoot 删除Redis指定缓存

    /* ******************************************载入缓存开始************************************************* ...

  3. VS2017 快捷键

    VS2017注释:先CTRL+K 然后CTRL+C   (ctrl按住不松,松开k按c) 取消注释:先CTRL+K,然后CTRL+U  (ctrl按住不松,松开k按c)

  4. vs2017,vs2019 无法连接到Web服务器“IIS Express”

    不知道啥原因,突然就不能访问了 我的解决方式: 在项目的根目录下显示所有隐藏的文件,找到.vs文件夹,删除: 重启项目,尝试运行,发现正常了. (完)

  5. CSS Sprites精灵图(雪碧图)

    简介 CSS精灵图,是一种网页图片应用处理方式.允许将一个页面涉及到的所有零星图片都包含到一张大图中 利用CSS的"background-image","backgrou ...

  6. zabbix fping 监控网络质量

    1,zabbix server (proxy)安装fping wget http://www.fping.org/dist/fping-3.16.tar.gz tar zxvf fping-3.16. ...

  7. 线性dp打鼹鼠

    题目大意 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个 的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气.你可 ...

  8. GitHub 热点速览 Vol.27:程序员的自我救赎——GitHub 摸鱼

    作者:HelloGitHub-小鱼干 摘要:都知道 VSCode 有各种摸鱼小插件,边听云音乐.边在 IDE 斗地主,再来一个 NBA 直播,怎一个美滋滋了得.作为 VSCode 的同门,GitHub ...

  9. pycharm设置字体和背景色

    Pycharm字体和背景色设置 1 菜单字体大小设置 设置后: 2.编辑字体大小设置 3.背景色设置

  10. Talk About AWS Aurora for MySQL max_connections parameter Calculation | 浅谈AWS Aurora for MySQL数据库中 max_connections参数的计算

    1. The Problem | 现象 When connect to the product environment database of my company, the Navicat show ...