Python:为什么我的 POST 请求不起作用?

标签 python json python-requests

我尝试使用 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 是否存在问题,或者我应该使用 selenium 吗?

编辑:我根据要求添加此代码。

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 中提取有关囚犯的信息,我建议使用类似 Beautiful Soup 的内容。这是一个 HTML 解析器,允许您提取任何您想要的信息。

关于Python:为什么我的 POST 请求不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55437088/

相关文章:

Python request.get 无法获得我可以在浏览器上打开的 url 的答案

用于上传到服务器的Python脚本

python - 在 Python 中实现 Github API

python - 问题爬取亚马逊,元素无法滚动到 View 中

java - 使用 Gson 库解析 Json

Python:构建 LRU 缓存

android - 在 Volley 中设置 cookie 不起作用

java - 在 JavaScript 中解析来自 Java Jackson JSON 的 JSON

javascript - 我的网站与另一个需要用户名和密码的网站之间的链接

python - Pandas lambda 多个参数返回