我正在尝试使用request.post在页面上提交表单,但所做的只是转到表单所在的页面。
http://www.gwinnettcountysheriff.com/smartwebclient/
def gwinnett_search(last, first, middle, booked):
headers = {'Content-type': "application/json",'accept':'application/json'}
payload = {'LastName': last, 'FirstName': first, 'MiddleName': middle, 'BeginBookDate': booked}
print("Gwinnett County Detention Center")
r = requests.post("http://www.gwinnettcountysheriff.com/smartwebclient/", json=payload, headers=headers)
print(r.status_code)
print(r.headers)
# print(r.text)
gwinnett_search(last, first, middle, option)
当我运行代码时,它返回以下内容(以及带有我要提交POST的表单的页面)。
Gwinnett County Detention Center
200
{'Cache-Control': 'private', 'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-IIS/8.5', 'X-AspNet-Version': '4.0.30319', 'X-Powered-By': 'ASP.NET', 'Date': 'Sun, 31 Mar 2019 01:07:32 GMT', 'Content-Length': '64441'}
Process finished with exit code 0
我不确定我在做什么错。我读到有人建议通过添加以下内容进行故障排除:
print(r.json())
返回:
Traceback (most recent call last):
{'Cache-Control': 'private', 'Content-Type': 'text/html; charset=utf-8', 'Content-Encoding': 'gzip', 'Vary': 'Accept-Encoding', 'Server': 'Microsoft-IIS/8.5', 'X-AspNet-Version': '4.0.30319', 'X-Powered-By': 'ASP.NET', 'Date': 'Sun, 31 Mar 2019 01:15:57 GMT', 'Content-Length': '64434'}
File "/home/john/PycharmProjects/inmate_search/inmate_search.py", line 58, in <module>
gwinnett_search(last, first, middle, option, booked)
File "/home/john/PycharmProjects/inmate_search/inmate_search.py", line 56, in gwinnett_search
print(r.json())
File "/home/john/PycharmProjects/inmate_search/venv/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
Process finished with exit code 1
也可能值得注意的是,当您使用搜索表单时,此站点上的URL不会更改。我看不到JSON是否有问题,还是应该只使用硒?
编辑:我正在按要求添加此代码。
last = input("Last Name:")
first = input("First Name: ")
middle = input("Middle Name: ")
booked = input("Booking Date(00/00/0000): ")
def options():
select = input("Enter 1 for In Custody or 2 for Inquiry:")
if select == "1":
select = "In+Custody"
elif select == "2":
select = "Inquiry"
else:
print("Error. That was not an option")
options()
return select
option = options()
答案 0 :(得分:3)
您没有做错任何事情。我去检查服务器,发现服务器已设置为POST请求返回HTML。但是,它不是与原始页面完全相同的HTML,因为返回的HTML仅包含有关您要搜索的囚犯的信息。
如果您想从此HTML中提取有关囚犯的信息,我建议使用Beautiful Soup之类的东西,它是HTML解析器,可让您提取所需的任何信息。