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还是比较有名和有意思的. 下面是百思不得姐的 ...
随机推荐
- 计算机网络之DDOS
1.什么是DDOS DDOS(Distributed Denial of Service),中文意思为“分布式拒绝服务”,就是利用大量合法的分布式服务器对目标发送请求,从而导致正常合法用户无法获得服务 ...
- 暑假集训Day2 互不侵犯(状压dp)
这又是个状压dp (大型自闭现场) 题目大意: 在N*N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. ...
- dart快速入门教程 (7.4)
7.12.多态 多态字面上理解就是多种状态,通俗的说,多态表现为父类定义一个方法不去实现,子类继承这个方法后实现父类的方法,这个方法有多种表现 // import 'person.dart'; voi ...
- windows10 通过vnc远程访问ubuntu16.04
参考链接 链接1 , 链接2 ,链接3 . 0.前言: 为方便深度学习训练,我们需要多个windows的电脑可以远程访问一个linux系统的工作站(以方便在linux系统上进行深度学习训练) 前提: ...
- 进阿里真的这么难?P8大佬告诉你,你和阿里之间缺的只是这份笔记
一转眼今年已经到六月份了,在这个过去的半年里有人选择了安稳,有的人偏偏不... 最近小编就有个朋友,去面了个[P8 级架构师],也算是摸摸行情,为后面的一些安排提前做好规划~ 先给大家介绍一下我这个朋 ...
- JIT的Profile神器JITWatch
简介 老是使用命令行工具在现代化社会好像已经跟不上节奏了,尤其是在做JIT分析时,使用LogCompilation输出的日志实在是太大了,让人望而生畏.有没有什么更加简便的方法来分析JIT日志呢?快来 ...
- 20 个 CSS高级样式技巧汇总
使用技巧会让人变的越来越懒,没错,我就是想让你变懒.下面是我收集的CSS高级技巧,希望你懒出境界. 1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate ...
- Redis基础01-redis的数据结构
参考书:<redis设计与实现> Redis虽然底层是用C语言写的,但是底层的数据结构并不是直接使用C语言的数据结构,而是自己单独封装的数据结构: Redis的底层数据结构由,简单动态字符 ...
- Python 之父说 Python 历史
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:鸿影洲冷 这篇文章主要内容来源于 Python 编程语言的最初设计者 ...
- Windows下生成IOS证书并发布APP安装到IPhone
目录: 一:生成证书 二:安装到IPhone 准备环境: 1.Appuploader(需要安装Java环境) 2.爱思助手 一.生成证书 1.1.打开appuploader后登陆开发者账号 1.2.点 ...