带有请求模块的Python POST请求



我正在尝试创建一个Python脚本,该脚本将填充数据库中的一些信息。对于服务器端,我使用了PHP,当我尝试使用浏览器提交信息时,它是有效的。但当我尝试使用下面的Python脚本时,它并没有。

import requests
url_insert = 'http://192.168.1.100/index.php'
data_insert = {'fullname':'spiderman',
'ssn':'1234',
'dept':'Security',
'salary':10000,
'homeaddress':'New York',
'btn_save':'Save'}
req = requests.post(url_insert, data = data_insert)
print(req.text)

响应:

Connected Successfully.<br><html>
<head>
<title>RETRIEVE DATA</title>
</head>
<body>
<form action="data.php" method="POST">
<div class="form-group">
<label for="id">Full Name</label>
<input type="text" name="fullname" id="fullname" value="" placeholder="FullName">
<br>
<br>
<label for="id">Social Security Number</label>
<input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number">
<br>
<br>
<label for="id">Department</label>
<input type="text" name="dept" id="dept" value="" placeholder="Department">
<br>
<br>
<label for="id">Salary</label>
<input type="text" name="salary" id="salary" value="" placeholder="Salary">
<br>
<br>
<label for="id">Address</label>
<input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address">
<br>
<br>
<input type="submit" name="btn_save" value="Save">
</div>
</form>
</body>
</html>

HTML代码:

<html>
<head>
<title>RETRIEVE DATA</title>
</head>
<body>
<form action="data.php" method="POST">
<div class="form-group">
<label for="id">Full Name</label>
<input type="text" name="fullname" id="fullname" value="" placeholder="FullName">
<br>
<br>
<label for="id">Social Security Number</label>
<input type="text" name="ssn" id="ssn" value="" placeholder="Social Security Number">
<br>
<br>
<label for="id">Department</label>
<input type="text" name="dept" id="dept" value="" placeholder="Department">
<br>
<br>
<label for="id">Salary</label>
<input type="text" name="salary" id="salary" value="" placeholder="Salary">
<br>
<br>
<label for="id">Address</label>
<input type="text" name="homeaddress" id="homeaddress" value="" placeholder="Address">
<br>
<br>
<input type="submit" name="btn_save" value="Save">
</div>
</form>
</body>
</html>

我是这方面的新手,任何帮助都将不胜感激。

我将假设,当您使用浏览器成功提交信息时,它是通过使用脚本index.php生成的表单。

该HTML表单将用户的数据输入到字段名(例如ssn(中,并将数据发布到脚本data.php中。您的Python脚本也应该将数据发布到data.php(但是,它似乎缺少fullname的数据(。但你却在index.php上发帖。然后,我希望响应是HTML表单,您之前已经成功地提交了信息。事实似乎确实如此。

只需将index.php更改为data.php,并为fullname提供一个值。

最新更新