python - Python将表单数据从CSV文件输入到本地HTML文件
问题描述
我目前正在尝试编写一个 python 脚本来从本地保存的 csv 文件中提取特定数据,然后将该数据输入到网站并单击提交。我已成功提取数据,但无法使用 mechanize 模块打开文件。
如果有另一种方法我只是认为 python 是完成这项任务的最简单方法,我愿意使用不同的语言。任何反馈将不胜感激。`
import datetime
import webbrowser
import csv
import mechanize
now = datetime.datetime.now()
mont = now.month
mon = mont + 2
with open(r'\\MyDataNEE\user$\bat0km4\Desktop\automation.csv') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
osha = mycsv[mon][2]
occasional = mycsv[mon][3]
fleet = mycsv[mon][4]
print(osha)
print(occasional)
print(fleet)
url = r"\\mydatanee\user$\bat0km4\Documents\test.html"
br = mechanize.Browser()
br.set_handle_robots( False )
br.open(url)
br.form['osh'] = osha
br.form['occasion'] = occasional
br.form['flee'] = fleet
br.submit()
` 更新:这是我的 html 代码,以防出现问题
<!DOCTYPE html>
<html>
<head></head>
<body>
<script type="text/javascript">
function show_confirm()
{
var r = confirm("Please Confirm that all values are correct. \nPress ok to submit");
if (r == true)
{
alert(document.getElementById('osh').value + " " + document.getElementById('occasion').value + " " + document.getElementById('flee').value);
return true;
}
else
{
return false;
}
}
</script>
<h1 align="center" style="color: black; font-size: 30pt; font-weight: bolder;">
Safety Indicator
</h1>
<form name="input" onsubmit="show_confirm();" method = "get">
<table align="center" border="0" cellspacing="4" cellpadding="0">
<tbody>
<tr>
<td>
OSHA Injuries
</td>
<td>
<input id = "osh" name="osha" type="text">
</td>
</tr>
<tr>
<td>
Occasional Use Incidents
</td>
<td>
<input id = "occasion" name="occasional" type="text">
</td>
</tr>
<tr>
<td>
Fleet Incidents
</td>
<td>
<input id = "flee" name="fleet" type="text">
</td>
</tr>
</tbody>
</table>
<div align="center">
<input type="submit" value="Submit">
</div>
</form>
</body>
</html>
更新:我使用了通过将 file:// 放在前面建议的文件类型,现在我收到了这个错误。我应该在我的 html 文件中放置一些东西以允许 mechanize 执行此操作吗? 谁能确认 mechanize 实际上具有与本地 html 文件交互的能力?我很难找到以这种方式使用机械化的人
C:\Python27>python pyth.py
0
1
2
Traceback (most recent call last):
File "pyth.py", line 24, in <module>
br.open(url)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 284, in _mech_open
response = UserAgentBase.open(self, request, data)
File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 195, in open
response = urlopen(self, req, data)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 352, in _open
'_open', req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 340, in _call_chain
result = func(*args)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1324, in file_open
return self.parent.open(req)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 284, in _mech_open
response = UserAgentBase.open(self, request, data)
File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 195, in open
response = urlopen(self, req, data)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 352, in _open
'_open', req)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 340, in _call_chain
result = func(*args)
File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1384, in ftp_open
raise URLError('ftp error: no host given')
解决方案
回答我最终下载了 Xampp,我可以使用 apache 在本地托管我的 html 文件,这解决了我的问题。我仍然想知道 mechanize 是否能够自行访问本地 html 文件。
推荐阅读
- javascript - 在反应中使用 setInterval 增加值
- spring - 从 Spring 安全密钥斗篷适配器注销,但无需登录即可访问应用程序
- php - 使用 Laravel APi 时搜索返回数据库中的所有数据
- sql - POSTGRES - 子查询返回结果非常慢
- laravel-5 - 无法在 Laravel 中创建主键-外键关系
- arm - 在 little endian cortex A 上运行 Big endian 进程
- c# - 开始使用 ODP for .NET 时遇到问题
- ios - NSAttributedString 设置字体大小 - swift
- android - 如何在 Recyclerview 中转到下一个选项卡 onClick?
- sql-server - 从两个表中选择数据,其中子表的两列具有相同的外键