我目前正在尝试编写一个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文件中以允许机械化这样做吗? 任何人都可以确认机械化确实能够与本地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')
答案 0 :(得分:0)
ANSWERED 我最终下载了Xampp,我可以使用apache在本地托管我的html文件,这解决了我的问题。我仍然想知道机械化是否能够自己访问本地html文件。