Python网络数据采集之登录窗口采集处理|第08天

时间:2022-05-30
本文章向大家介绍Python网络数据采集之登录窗口采集处理|第08天,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

利用Requests库处理Cookiesession等方式的登录。

登录窗口采集处理

如果我们采集的网站需要我们登录后才能获取我们想要的数据,这就需要进一步处理登录这个问题。

登录的原理很简单,即前台向服务器传输数据进行验证。传输的方式有很多种,例如GETPOST;页面表单基本上可以看成是一种用户提交 POST请求的方式,且这种请求方式是服务器能够理解和使用的。

Python Requests库

除了Python的标准库urllib库,还有第三方库可以选择,例如:Requests。主要擅长处理那些复杂的 HTTP 请求、cookieheader(响应头和请求头)等内容的 Python第三方库。

项目地址:https://github.com/kennethreitz/requests/

安装的方式也很简单。例如pip安装,或者下载源码安装。

源码地址:https://github.com/kennethreitz/requests/tarball/master

提交表单

提交表单一般是HTML的方式可以实现,且大多也采用这样的方式进行提交。例如:

<form method="post" action="processing.php">
Nickename: <input type="text" name="nickename"><br> 
username: <input type="text" name="username"><br> 
<input type="submit" value="Submit">
</form>

我们Python的用Requests库来提交十分简单。

import requests
params = {'name': 'Ryan', 'username': 'Mitchell'}
r = requests.post("http://pythonscraping.com/files/processing.php", data=params)
print(r.text)

单选按钮、复选框等输入

无论表单的字段看起来多么复杂,仍然只有两件事是需要关注的:字段名称和值。字段名称可以通过查看源代码寻找name 属性轻易获得。而字段的值有时会比较复杂,有可能是在表单提交之前通过 JavaScript 生成的。

我们可以通过抓包或者浏览器的网络请求信息来判断,例如:

1

https:chensenlin.cn?c=hello&m=senlin

Python需要理解为:

1

{'c':'hello','m':'senlin'}

具体查看方法可以参考下图所示:

提交文件或者图像

HTML提交文件的时候,需要添加一个参数enctype="multipart/form-data",声明这是文件上传的类型。同时inputtypefile

<from action="uploadFile.php" metoh="post"  enctype="multipart/form-data">
uploadFile:<input type="file" name="filename">
提交:<input type="submit" value="上传">
</from>

同理,Python Requests 库对这种表单的处理方式如下:

import requests
     files = {'uploadFile': open('../files/Python-logo.png', 'rb')}
     r = requests.post("https:chensenlin.cn?c=filename&m=upload",files=files)

print(r.text)

处理登录和Cookie

网站大多都用 cookie 跟踪用户是否已登录的状态信息。一旦网站验证了你的登录权证,它就会将它们保存在你的浏览器的 cookie 中,里面通常包含一个服务器生成的令牌、登录有效时限和状态跟踪信息。网站会把这个cookie当作信息验证的证据,在你浏览网站的每个页面时出示给服务器。

根据我们上面的逻辑用Requests库跟踪cookie的代码示例也比较简单:

import requests

params = {'username': 'demochen', 'password': 'password'}

r = requests.post("http://pythonscraping.com/pages/cookies/welcome.php", params)
print("Cookie is set to:")
print(r.cookies.get_dict())
print("-----------")
print("Going to profile page...")
r = requests.get("http://pythonscraping.com/pages/cookies/profile.php",cookies=r.cookies)
print(r.text)

不过也有session的方式进行登录,但是Requests库的session函数处理也很方便。具体和cookie类似,不过多阐述,或者查看文档了解也可以。

import requests
session = requests.Session()
params = {'username': 'username', 'password': 'password'}
s = session.post("http://pythonscraping.com/pages/cookies/welcome.php", params)
print("Cookie is set to:")
print(s.cookies.get_dict())
print("-----------")
print("Going to profile page...")
s = session.get("http://pythonscraping.com/pages/cookies/profile.php") 
print(s.text)

值得注意的是,登录还有一种是用HTTP基本接入认证的方式。Requests库有一个 auth模块专门用来处理 HTTP 认证:

import requests
from requests.auth import AuthBase
from requests.auth import HTTPBasicAuth
     auth = HTTPBasicAuth('ryan', 'password')
     r = requests.post(url="http://pythonscraping.com/pages/auth/login.php", auth=
auth)
print(r.text)