当前位置:嗨网首页>书籍在线阅读

32-第2步_建立坐标

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

第2步:建立坐标

访问第一步中的表单网站,在浏览器中载入你下载的示例表单(如图20-7所示)。

让你的源代码看起来像下面的样子:

#! python3
# formFiller.py - Automatically fills in the form. 
import pyautogui, time
# TODO: Give the user a chance to kill the script.
# TODO: Wait until the form page has loaded.
# TODO: Fill out the Name Field.
# TODO: Fill out the Greatest Fear(s) field.
# TODO: Fill out the Source of Wizard Powers field.
# TODO: Fill out the RoboCop field.
# TODO: Fill out the Additional Comments  field.
#  TODO:  Click  Submit.
# TODO: Wait until form page has loaded.
# TODO:  Click the Submit another response link.

现在你需要实际想要输入这张表格的数据。在真实世界中,这些数据可能来自电子表格、纯文本文件或某个网站。可能需要编写额外的代码,将数据加载到程序中。但对于这个项目,只需要将这些数据硬编码给一个变量。在程序中加入以下代码:

#! python3
# formFiller.py - Automatically fills in the form.
--snip--
formData = [{'name': 'Alice', 'fear': 'eavesdroppers', 'source': 'wand',
 'robocop': 4, 'comments': 'Tell Bob I said hi.'},
 {'name': 'Bob', 'fear': 'bees', 'source': 'amulet', 'robocop': 4,
 'comments': 'n/a'},
 {'name': 'Carol', 'fear': 'puppets', 'source': 'crystal ball',
 'robocop': 1, 'comments': 'Please take the puppets out of the
 break room.'},
 {'name': 'Alex Murphy', 'fear': 'ED-209', 'source': 'money',
 'robocop': 5, 'comments': 'Protect the innocent. Serve the public
 trust. Uphold the law.'},
 ]
--snip--

formData 列表包含4个字典,针对4个不同的名字。每个字典都有文本字段的名字作为键,响应作为值。最后一点准备是设置PyAutoGUI 的 PAUSE 变量,在每次函数调用后等待半秒。在程序的 formData 赋值语句后,添加下面的代码:

pyautogui.PAUSE  =  0.5
print('Ensure that the browser window is active and the form is loaded!')