python - 如何使用 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)
谢谢
解决方案
你的格式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
推荐阅读
- html - 在ajax成功后使用td将文本添加到span标签中
- c++ - uintptr_t 是否存储指针的地址或其值?
- typescript - 为 Intern 4 功能测试自动生成文档
- laravel-5 - 在嵌套关系中传递记录 ID 以更新表单
- php - 中的 PHP 变量
- java - Java send notification to all classes
- php - Best way to search multiple fields laravel
- machine-learning - SVM 可视化非常随机且不准确
- ios - 在 Context 上应用回滚时使用 NSManagedObjectContextDidSaveNotification 回滚更新信息
- node.js - Error in retrieving response from firebase cloud function