介绍

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. junit基本介绍视频笔记1

    程序员每天工作的基本流程: 1.从svn检出代码: 2.运行单元测试,测试无误,进入下一步: 3.开始一天的代码编写工作: 4.代码提交到服务器之前进行单元测试: 5.单元测试通过提交到svn服务器. ...

  2. 微信小程序navigator带参数跳转及接收参数内容

    // index.wxml <navigator class='looks-view' wx:for="{{imgUrlNew}}" wx:key="index&q ...

  3. Docker(五)Docker镜像讲解

    Docker镜像讲解 镜像概念 镜像是一种轻量级.可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,包括代码.运行时.库.环境变量和配置文件 Dock ...

  4. Hystrix入门教程

    Hystrix入门教程 一·什么是Hystrix?Hystrix有什么作用?使用Hystrix有哪些适用场景 Hystrix是springCloud的组件之一,Hystrix 可以让我们在分布式系统中 ...

  5. node+ajax实战案例(2)

    2.静态资源渲染 2.1.创建http服务器 var http = require('http'); var url = require('url'); var app = http.createSe ...

  6. linux test tool--"ab"

    install(ubuntu os): sudo apt-get install apache2-utils usage: ab -c 200 -t 100 http://localhost:8001 ...

  7. python实现的udp-收发聊天器

    构建思想:创建三个函数,最后一个函数调用前两个 1.创建发送函数-send() 2.创建接收函数-recv() 3.创建调用函数(主函数)-main() import socket def send( ...

  8. 痞子衡嵌入式:轻松为i.MXRT设计更新Segger J-Link Flash下载算法文件

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是为i.MXRT设计更新Segger J-Link Flash下载算法文件. 想要在Flash中调试,基本是离不开Flash下载算法的,毕 ...

  9. JAVASE经典面试问题(必须熟背),你Get到了吗?

    JAVASE经典面试问题(必须熟背) 1. 编译java程序使用什么命令?运行java使用什么命令? javac *.java java 类名 2. 什么是JDK,什么是JRE,JDK与JRE有什么区 ...

  10. Python爬虫:手把手教你写迷你爬虫架构

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:我爱学Python 语言&环境 语言:继续用Python开路 ...