首页 > 解决方案 > 如何使用 Google Colab 在 Python 中执行此 POST 请求。我目前收到 400 响应代码

问题描述

我正在尝试提取此网站上所有工作的信息: https ://www.americanmobile.com/travel-nursing-jobs/search/

查看网络活动选项卡,看起来我需要的所有数据都来自此处发出的 POST 请求: https ://jobs.amnhealthcare.com/api/jobs//search 。我附上了一张图片,可以帮助确认我所引用的确切内容。 示例_1

我在 Google Colab 中编写了以下代码,以尝试至少获得前 10 个结果。使用标头和参数引用python 请求 POST ,我知道很多标头甚至可能都不是必需的。我试过发送这个请求,根本没有任何标题。

我想要做的甚至可能吗?到目前为止,我只收到了 400 响应代码。

如果可以做到这一点,是否可以为所有 4k + 工作提取此信息?

import requests
import re
payload = {
  "PageNumber": "1",
    "JobsPerPage": "10",
    "Filters": {
        "Locations": "[]",
        "Skillset": "[]",
        "StartDates": "[]",
        "Shifts": "[]",
        "Durations": "[]",
        "IsCovid19Job": "false",
        "Exclusive": "false",
        "Skillsets": "[]",
        "DivisionCompanyId": "2",
        "LocationSearch": "",
        "KeywordSearch": "",
        "DaxtraJobIds": "[]"
    },
    "SortOrder": {
        "Header": "MaxPayRate",
        "SortDirection": "Descending"
    }
}

headers = {
    "Host": "jobs.amnhealthcare.com",
    "Connection": "keep-alive",
    "Content-Length": "315",
    "Accept": "application/json, text/plain, */*",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "Content-Type": "application/json;charset=UTF-8",
    "Origin": "https://www.americanmobile.com",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://www.americanmobile.com/",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9"
}
response = requests.post('https://jobs.amnhealthcare.com/api/jobs//search' , data=p , headers=headers)

谢谢

标签: pythongoogle-colaboratory

解决方案


你的格式data不完全正确。这应该有效:

import requests

headers = {
    "Host": "jobs.amnhealthcare.com",
    "Connection": "keep-alive",
    "Content-Length": "315",
    "Accept": "application/json, text/plain, */*",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
    "Content-Type": "application/json;charset=UTF-8",
    "Origin": "https://www.americanmobile.com",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Dest": "empty",
    "Referer": "https://www.americanmobile.com/",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9"
}

data = '{"PageNumber":1,"JobsPerPage":10,"Filters":{"Locations":[],"Skillset":[],"StartDates":[],"Shifts":[],"Durations":[],"IsCovid19Job":false,"Exclusive":false,"Skillsets":[],"DivisionCompanyId":2,"LocationSearch":"","KeywordSearch":"","DaxtraJobIds":[]},"SortOrder":{"Header":"MaxPayRate","SortDirection":"Descending"}}'

response = requests.post('https://jobs.amnhealthcare.com/api/jobs//search', headers=headers, data=data)

您现在可以在 for 循环中调整JobsPerPage和检索您需要的所有帖子。PageNumber


推荐阅读