Python爬虫登录后token处理

news/2024/7/19 12:44:37 标签: python, 爬虫, token, 编程, 开发

今天继续给大家介绍Python爬虫相关知识,本文主要内容是Python爬虫登录后token处理。

tokentoken_1">一、网页tokentoken作用

在上文Python爬虫登录后cookie处理中,我们介绍过使用使用Python爬虫解决cookie及网页登录访问问题。
然而,有的网站,例如DVWA,为了防止CSRF等攻击(CSRF漏洞是常见的安全漏洞之一,关于CSRF可以参考文章:CSRF漏洞简介),会设置token字段。
我们可以使用浏览器开发者工具检查登录页面HTML源代码如下所示:
在这里插入图片描述
从上图中可以看出,在页面中有一个隐藏的input标签,因为该标签属性设置位了hidden,因此该标签不会显示在浏览器中,不会影响页面整体效果。但是该input标签是存在name和value的,因此当我们点击浏览器中的Login按钮时,该input标签中的name和value也会被当作POST提交数据的一部分进行提交。最麻烦的是,该字段的值会随着我们对页面的刷新而变化,这意味着我们无法从浏览器复制该值后粘贴到Python代码中!

token_7">二、Python爬虫token问题解决

为了解决这一问题,我们就必须先访问站点的登录页面,获取token值,在获取token值之后,将其作为登录数据包的POST提交数据的一部分,与用户名和密码一起提交。这样就解决了token的验证问题。
针对DVWA网站,我们实现的简单登录后爬取主页数据Python爬虫代码如下所示:

python">token keyword">import requests
token keyword">from lxml token keyword">import etree

url1token operator">=token string">"http://192.168.136.1/dvwa/login.php"
url2token operator">=token string">"http://192.168.136.1/dvwa/index.php"
headerstoken operator">=token punctuation">{token string">"User-Agent"token punctuation">:token string">"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"token punctuation">}

sessiontoken operator">=requeststoken punctuation">.sessiontoken punctuation">(token punctuation">)
response1token operator">=sessiontoken punctuation">.gettoken punctuation">(urltoken operator">=url1token punctuation">,headerstoken operator">=headerstoken punctuation">)
page_text1token operator">=response1token punctuation">.text
treetoken operator">=etreetoken punctuation">.HTMLtoken punctuation">(page_text1token punctuation">)
tokentoken operator">=treetoken punctuation">.xpathtoken punctuation">(token string">'//*[@id="content"]/form/input/@value'token punctuation">)token punctuation">[token number">0token punctuation">]
token keyword">printtoken punctuation">(tokentoken punctuation">)
datatoken operator">=token punctuation">{
    token string">"username"token punctuation">:token string">"admin"token punctuation">,
    token string">"password"token punctuation">:token string">"password"token punctuation">,
    token string">"Login"token punctuation">:token string">"login"token punctuation">,
    token string">"user_token"token punctuation">:token
token punctuation">}
response2token operator">=sessiontoken punctuation">.posttoken punctuation">(urltoken operator">=url1token punctuation">,headerstoken operator">=headerstoken punctuation">,datatoken operator">=datatoken punctuation">)
response3token operator">=sessiontoken punctuation">.gettoken punctuation">(urltoken operator">=url2token punctuation">,headerstoken operator">=headerstoken punctuation">)
page_texttoken operator">=response3token punctuation">.text
fptoken operator">=token builtin">opentoken punctuation">(token string">"./dvwa.html"token punctuation">,token string">'w'token punctuation">)
fptoken punctuation">.writetoken punctuation">(page_texttoken punctuation">)
fptoken punctuation">.closetoken punctuation">(token punctuation">)

在上述代码中,我们一共发起了3次session请求,第一次请求是为了获取token,第二次请求是为了成功登录,并获取登录后的cookie,第三次请求是为了拿到登录后的主页数据。上述代码执行后,我们成功下载到了登录后的源码数据,结果如下所示:
在这里插入图片描述
从上图可以看出,我们使用Python爬虫处理token成功!
原创不易,转载请说明出处:https://blog.csdn.net/weixin_40228200


http://www.niftyadmin.cn/n/24978.html

相关文章

vector使用指南

目录 引言 空间配置器 vector<char>与 string的一些差异 vector容器与string容器的一些差异 接口介绍——reserve resize接口 shrink_to_fit 接口 operator[ ] 和 at 接口 assign接口 增删查改接口 swap接口 例题讲解 引言 vector实质上就是数据结构的顺序表…

软测复习01:软件测试概述

文章目录软件测试的目的软件测试的定义软件测试与软件开发软件测试发展软件测试的目的 基于不同的立场&#xff0c;存在着两种完全不同的测试目的 从用户的角度出发&#xff0c;希望通过软件测试暴露软件中隐藏的错误和缺陷&#xff0c;以考虑是否可接受该产品。从软件开发者的…

LeetCode 138. 复制带随机指针的链表(C++)

思路&#xff1a; 用哈希表实现&#xff0c;创建一个哈希表来对应原链表中的每一个节点&#xff0c;这样也可以将原链表中的所有结点的next和random关系映射到哈希表复制链表中。 原题链接&#xff1a;https://leetcode.cn/problems/copy-list-with-random-pointer/description…

C语言灵魂核心——指针深度修炼

&#x1f412;个人主页&#xff1a;平凡的小苏&#x1f4da;学习格言&#xff1a;别人可以拷贝我的模式&#xff0c;但不能拷贝我不断往前的激情目录 1. 字符指针 2. 指针数组 3. 数组指针 3.1 数组指针的定义 3.2 &数组名VS数组名 3.3 数组指针的使用 4. 数组参数、…

『年度总结』时光如梭 | 再见 2022 | 你好 2023

⭐创作时间2022年12月31日⭐ ✨结果一直到现在才发&#xff0c;说真的写年度总结还是第一次写比较不熟练&#xff0c;去年有这个活动也有佬叫我参加&#xff0c;不过没参加。今年想着有时间来写下的&#xff0c;结果写到现在才发&#xff0c;这东西说真的挺难写的&#…

(1分钟了解)SLAM的七大问题:地图表示、信息感知、数据关联、定位与构图、回环检测、深度、绑架

编辑切换为居中添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09;SLAM问题也被称为是CML问题。编辑切换为居中添加图片注释&#xff0c;不超过 140 字&#xff08;可选&#xff09;编辑切换为居中添加图片注释&#xff0c;不超过 140 字&#xff08;可选&…

【PTA-训练day24】2022 天梯模拟赛L1刷题记录

目录 L1-118 均是素数 - 20 L1-117 矩阵列平移 - 20 L1-107 检查密码 - 15 L1-106 判断题 - 15 L1-112 不变初心数 - 15 L1-116 字母串 - 15 L1-118 均是素数 - 20 #include <bits/stdc.h> using namespace std;bool isprime(int x) {if(x<2) return false;for…

java面试题每日10问(1)

Core Java: Basics of Java Interview Questions 1.What is Java? Java is object-oriented, platform-independent, Multithreaded, and portable programming language.it provides its own JRE and API. 2.What is the difference between JDK, JRE, and JVM? JVM Java…