【发布时间】:2018-11-03 17:08:43
【问题描述】:
我目前正在尝试编写一个 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>
UPDATE :我使用了通过将 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')
【问题讨论】:
-
请将错误粘贴为代码 sn-ps 而不是图像
-
添加您遇到的错误
-
路径为本地UNC网络路径。将
file://添加到url(不是http)。您可以将所有斜杠交换为/。例如。file://///mydatanee/user$/bat0km4/Documents/test.html
标签: python html forms input mechanize