python - Python:为什么我的 POST 请求不起作用?
问题描述
我正在尝试使用 requests.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()
解决方案
你没有做错什么。我去检查服务器,发现服务器设置为使 POST 请求返回 HTML。但是,它与原始页面的 HTML 并不完全相同,因为此返回的 HTML 仅包含有关您正在搜索的囚犯的信息。
如果你想从这个 HTML 中提取有关囚犯的信息,我建议使用Beautiful Soup之类的东西,它是一个 HTML 解析器,可以让你提取任何你想要的信息。
推荐阅读
- javascript - 使用 CLI package.json 脚本时生产部署失败
- azure - 在 Azure 上规划 Kubernetes 集群
- java - 满足一定条件如何遍历节点列表并替换内容?
- android - 具有多个 Activity 的 Android 导航抽屉
- swift - 与视觉暂留相匹配的颜色选择 两种颜色混合
- ruby - Capybara / Ruby - 尝试仅从所有模棱两可的 css 选择器中获取文本并将其转换为字符串
- python - 从 Google Cloud Function 访问 SIFT
- javascript - 我可以扩展我的 div,但如何缩小它?
- laravel - 同时使用 web 和 token api gaurd 时的异常处理
- vim - Vim autocmd 没有正确映射映射