Python爬虫笔记【一】模拟用户访问之表单处理(3)

随声附和 提交于 2019-11-29 14:18:37

学习的课本为《python网络数据采集》,大部分代码来此此书。

  大多数网页表单都是由一些HTML 字段、一个提交按钮、一个在表单处理完之后跳转的“执行结果”(表单属性action 的值)页面构成。虽然这些HTML 字段通常由文字内容构成,但是也可以实现文件上传或其他非文字内容。这些都为抓取数据的阻碍所以放在了前面。废话不多说开搞。

  1.HTTP基本接入认证

在发明cookie 之前,处理网站登录最常用的方法就是用HTTP 基本接入认证(HTTP basicaccess authentication)

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)

虽然这看着像是一个普通的POST 请求,但是有一个HTTPBasicAuth 对象作为auth 参数传递到请求中。显示的结果将是用户名和密码验证成功的页面(如果验证失败,就是一个拒绝接入页面)。

  2.一般表单处理

表单源码:

<form method="post" action="processing.php">
  First name: <input type="text" name="firstname"><br>
  Last name: <input type="text" name="lastname"><br>
  <input type="submit" value="Submit">
</form>

注意一下:首先,两个输入字段的名称是firstname 和lastname,这一点非常重要。字段的名称决定了表单被确认后要被传送到服务器上的变量名称。如果你想模拟表单提交数据的行为,你就需要保证你的变量名称与字段名称是一一对应的。其次注意表单的真实行为其实发生在processing.php(绝对路径是http://pythonscraping.com/files/processing.php)。表单的任何POST 请求其实都发生在这个页面上,并非表单本身所在的页面。

python语句:

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

  3.看个代码稍微多点的

并不需要看懂每个代码的意思,只要知道你需要那些信息就可以(这句写在前面)

html:

<form action="http://post.oreilly.com/client/o/oreilly/forms/quicksignup.cgi" id="example_form2" method="POST">
  <input name="client_token" type="hidden" value="oreilly" />
  <input name="subscribe" type="hidden" value="optin" />
  <input name="success_url" type="hidden" value="http://oreilly.com/store/newsletter-thankyou.html" />
  <input name="error_url" type="hidden" value="http://oreilly.com/store/newsletter-signup-error.html" />
  <input name="topic_or_dod" type="hidden" value="1" />
  <input name="source" type="hidden" value="orm-home-t1-dotd" />
  <fieldset>
    <input class="email_address long" maxlength="200" name="email_addr" size="25" type="text"         value="Enter your email here" />
    <button alt="Join" class="skinny" name="submit" onclick="return addClickTracking('orm','ebook','rightrail','dod');        " value="submit">Join</button>
  </fieldset>
</form>

虽然第一次看这些会觉得恐怖,但是大多数情况下(后面我们会介绍异常)你只需要关注两件事:
  • 你想提交数据的字段名称(在这个例子中是email_addr)
  • 表单的action 属性,也就是表单提交后网站会显示的页面(在这个例子中是http://post.oreilly.com/client/o/oreilly/forms/quicksignup.cgi)把对应的信息增加到请求信息中,运行代码即可:

import requests
params = {'email_addr': 'ryan.e.mitchell@gmail.com'}
r = requests.post("http://post.oreilly.com/client/o/oreilly/forms/
quicksignup.cgi", data=params)
print(r.text)

  4.提交文件和图像

html:

<h2>Upload a file!</h2>
<form action="../pages/files/processing2.php" method="post" enctype="multipart/form-data">
  Submit a jpg, png, or gif: <input type="file" name="uploadFile"><br>
  <input type="submit" value="Upload File">
</form>

python:

import requests
files = {'uploadFile': open('../files/Python-logo.png', 'rb')}
r = requests.post("http://pythonscraping.com/pages/processing2.php",
files=files)
print(r.text)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!