【问题标题】:Unable to send an application in the right way using post requests having multiple parameters无法使用具有多个参数的发布请求以正确的方式发送应用程序
【发布时间】:2020-04-10 08:47:37
【问题描述】:

我正在尝试在使用 python 填写网页中可用的表单后发送应用程序。我试图模仿我在 chrome 开发工具中看到的过程,但似乎我在某个地方出错了,这就是我在执行以下脚本时收到此错误的原因:

{
"message":"415 Unsupported Media Type returned for /apply-app/rest/jobs/PIDFK026203F3VBQB79V77VIY-87592/submissions with message: ",
"key":"Exception_server_error",
"errorId":"d6b128bd-426d-4bee-8dbb-03e232829f5e"
}

在我看来,我需要以自动方式使用 tokenversion 的值,因为它们在每个应用程序中都不同,但我在页面源代码和内容中找不到它们。

我选择了No 作为Additional Information 中所有下拉菜单(如果有的话)的值。

Link到申请页面

Link 到我用过三次的附件。

我试过了:

import requests

main_link = "https://karriere.hsbc.de/stellenangebote/stellenboerse/apply?jobId=PIDFK026203F3VBQB79V77VIY-87592&langCode=de_DE"
post_link = "https://emea3.recruitmentplatform.com/apply-app/rest/jobs/PIDFK026203F3VBQB79V77VIY-87592/submissions"

payload = {
    "candidateIdentity":{"firstName":"syed","lastName":"mushfiq","email":"mthmt80@gmail.com"},
    "answeredDocuments":[{"documentType":"answeredForm","formId":"hsbc_bewerbungsprozess_pers_nliche_daten",
    "answers":[
    {"questionId":"form_of_address","type":"options","value":["form_of_address_m"]},
    {"questionId":"academic_title","type":"simple","value":" Dr.","questionIds":[]},
    {"questionId":"first_name","type":"simple","value":"syed","questionIds":[]},
    {"questionId":"last_name","type":"simple","value":"mushfiq","questionIds":[]},
    {"questionId":"e-mail_address","type":"simple","value":"mthmt80@gmail.com","questionIds":[]},
    {"questionId":"phone__mobile_","type":"phone","countryCode":"+880","isoCountryCode":"BD","subscriberNumber":"1790128884"}]},
    {"documentType":"answeredForm","formId":"hsbc_bewerbungsprozess_standard_fragebogen","answers":[{"questionId":"custom_question_450","type":"options","value":["custom_question_450_ja"]},
    {"questionId":"custom_question_451","type":"options","value":["custom_question_451_nein"]},
    {"questionId":"custom_question_452","type":"options","value":["custom_question_452_unter_keine_der_zuvor_genannten"]},
    {"questionId":"custom_question_580","type":"options","value":["custom_question_580_nein_978"]},
    {"questionId":"custom_question_637","type":"options","value":["custom_question_637_nein"]},
    {"questionId":"custom_question_579","type":"options","value":["custom_question_579_nein"]},
    {"questionId":"custom_question_583","type":"options","value":["custom_question_583_hsbc_deutschland_karriereseite"]}]},

    #============The following three lines are supposed to help upload three files============

    {"documentType":"attachment","attachmentId":"cover_letter","token":"2d178469-cdb5-4d65-9f67-1e7637896953","filename": open("demo.pdf","rb")},
    {"documentType":"attachment","attachmentId":"attached_resume","token":"81a5a661-66bb-4918-a35c-ec260ffb7d02","filename": open("demo.pdf","rb")},
    {"documentType":"attachment","attachmentId":"otherattachment","token":"4c3f7500-b072-48d4-83cf-0af1399bc8ba","filename": open("demo.pdf","rb")}],

    #============The version's value should not be hardcoded=========================

    "version":"V2:3:14dfac80702d099625d0274121b0dba68ac0fd96:861836b7d86adae8cc1ce69198b69b8ca59e2ed5","lastModifiedDate":1562056029000,"answeredDataPrivacyConsents":[{"identifier":"urn:lms:ta:tlk:data-privacy-consent:mtu531:101","consentProvided":True},
    {"identifier":"urn:lms:ta:tlk:data-privacy-consent:mtu531:102","consentProvided":True}],
    "metaInformation":{"applicationFormUrl":"https://karriere.hsbc.de/stellenangebote/stellenboerse/apply?jobId=PIDFK026203F3VBQB79V77VIY-87592&langCode=de_DE","jobsToLink":[]}
}

def send_application(s,link):   
    res = s.post(link,data=payload)
    print(res.text)

if __name__ == '__main__':
    with requests.Session() as s:
        send_application(s,post_link)

如何以正确的方式发送申请?

PS 我可以使用相同的文档多次手动将申请发送到同一个电子邮件。

【问题讨论】:

  • 您愿意接受基于硒的方法吗?它不会像请求一样快,但会节省您的开发时间。
  • 感谢您对@Chandan Gm 的关注。如果我还没有得到基于请求的解决方案,我会选择硒作为最后的手段。看来 bmcculley 提供的解决方案正在以正确的方式工作。

标签: python python-3.x post web-scraping python-requests


【解决方案1】:

进行此类操作的最佳方法是在浏览器中打开页面并在开发人员工具中查看网络选项卡。从那里填写表格时,您将能够看到每次附加文档时它都会发送 ajax 请求并在 json 响应中接收令牌。使用这些令牌,您可以构建应以 json 格式提交的最终有效负载。

下面是一些有效的示例代码:

import requests

headers = {
    'Host': 'emea3.recruitmentplatform.com',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'en-US,en;q=0.5',
    'Accept-Encoding': 'gzip, deflate',
    'apply-config-key': 'AAACEwAA-55cd88d4-c9fd-41ce-95a4-f238402b898f',
    'Origin': 'https://karriere.hsbc.de',
    'DNT': '1',
    'Connection': 'close',
    'Referer': 'https://karriere.hsbc.de/',
    'Cookie': 'lumesse_language=de_DE'
}

main_link = "https://karriere.hsbc.de/stellenangebote/stellenboerse/apply?jobId=PIDFK026203F3VBQB79V77VIY-87592&langCode=de_DE"
post_link = "https://emea3.recruitmentplatform.com/apply-app/rest/jobs/PIDFK026203F3VBQB79V77VIY-87592/submissions"
ajax_link = "https://emea3.recruitmentplatform.com/apply-app/rest/jobs/PIDFK026203F3VBQB79V77VIY-87592/attachments"

def build_payload(cover_letter_token, attached_resume_token, otherattachment_token):
    return {
      "candidateIdentity": {
        "firstName": "Syed",
        "lastName": "Mushfiq",
        "email": "mthmt80@gmail.com"
      },
      "answeredDocuments": [
        {
          "documentType": "answeredForm",
          "formId": "hsbc_bewerbungsprozess_pers_nliche_daten",
          "answers": [
            {
              "questionId": "form_of_address",
              "type": "options",
              "value": [
                "form_of_address_m"
              ]
            },
            {
              "questionId": "academic_title",
              "type": "simple",
              "value": "prof",
              "questionIds": []
            },
            {
              "questionId": "first_name",
              "type": "simple",
              "value": "Syed",
              "questionIds": []
            },
            {
              "questionId": "last_name",
              "type": "simple",
              "value": "Mushfiq",
              "questionIds": []
            },
            {
              "questionId": "e-mail_address",
              "type": "simple",
              "value": "mthmt80@gmail.com",
              "questionIds": []
            },
            {
              "questionId": "phone__mobile_",
              "type": "phone",
              "countryCode": "+49",
              "isoCountryCode": "DE",
              "subscriberNumber": "30 33850062"
            }
          ]
        },
        {
          "documentType": "answeredForm",
          "formId": "hsbc_bewerbungsprozess_standard_fragebogen",
          "answers": [
            {
              "questionId": "custom_question_450",
              "type": "options",
              "value": [
                "custom_question_450_ja"
              ]
            },
            {
              "questionId": "custom_question_451",
              "type": "options",
              "value": [
                "custom_question_451_nein"
              ]
            },
            {
              "questionId": "custom_question_452",
              "type": "options",
              "value": [
                "custom_question_452_unter_keine_der_zuvor_genannten"
              ]
            },
            {
              "questionId": "custom_question_580",
              "type": "options",
              "value": [
                "custom_question_580_ja"
              ]
            },
            {
              "questionId": "custom_question_637",
              "type": "options",
              "value": [
                "custom_question_637_nein"
              ]
            },
            {
              "questionId": "custom_question_579",
              "type": "options",
              "value": [
                "custom_question_579_nein"
              ]
            },
            {
              "questionId": "custom_question_583",
              "type": "options",
              "value": [
                "custom_question_583_linkedin"
              ]
            }
          ]
        },
        {
          "documentType": "attachment",
          "attachmentId": "cover_letter",
          "token": cover_letter_token,
          "filename": "demo.pdf"
        },
        {
          "documentType": "attachment",
          "attachmentId": "attached_resume",
          "token": attached_resume_token,
          "filename": "demo.pdf"
        },
        {
          "documentType": "attachment",
          "attachmentId": "otherattachment",
          "token": otherattachment_token,
          "filename": "demo.pdf"
        }
      ],
      "version": "V2:3:14dfac80702d099625d0274121b0dba68ac0fd96:861836b7d86adae8cc1ce69198b69b8ca59e2ed5",
      "lastModifiedDate": "1562056029000",
      "answeredDataPrivacyConsents": [
        {
          "identifier": "urn:lms:ta:tlk:data-privacy-consent:mtu531:101",
          "consentProvided": "true"
        },
        {
          "identifier": "urn:lms:ta:tlk:data-privacy-consent:mtu531:102",
          "consentProvided": "true"
        }
      ],
      "metaInformation": {
        "applicationFormUrl": "https://karriere.hsbc.de/stellenangebote/stellenboerse/apply?jobId=PIDFK026203F3VBQB79V77VIY-87592&langCode=de_DE",
        "jobsToLink": []
      }
    }

def submit_attachment(s, link, f):
    d = open(f, 'rb').read()
    r = s.post(link, files={'file':('demo.pdf', d),'applicationProcessVersion':(None, 'V2:3:14dfac80702d099625d0274121b0dba68ac0fd96:861836b7d86adae8cc1ce69198b69b8ca59e2ed5')})
    r_data = r.json()
    return r_data.get('token')

def send_application(s,link,p):   
    res = s.post(link, json=p)
    return res

if __name__ == '__main__':
    attachment_list = ["cover_letter_token", "attached_resume_token", "otherattachment_token"]
    token_dict = {}

    with requests.Session() as s:
        s.headers.update(headers)

        for at in attachment_list:
            rt = submit_attachment(s, ajax_link, "demo.pdf")
            token_dict[at] = rt

        payload = build_payload(token_dict['cover_letter_token'], token_dict['attached_resume_token'], token_dict['otherattachment_token'])

        rd = send_application(s, post_link, payload)
        print(rd.text)
        print(rd.status_code)

【讨论】:

  • 它似乎以正确的方式工作@bmcculley。如果你在你的解决方案中做一个小的改动,那就太好了,这样我就可以使用三个不同的 pdf 文件,如first_demo.pdfsecond_demo.pdfthird_demo.pdf,而不仅仅是demo.pdf。谢谢。
  • 嗨@bmcculley,我已经修改了this 以利用所有三个pdf文件,我发现它工作正常。我没有以正确的方式进行修改吗?谢谢。
  • 如果有任何解决方案可以提供@bmcculley,你可以试试this post。谢谢。
猜你喜欢
  • 2022-11-28
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 2020-11-27
  • 2017-10-26
  • 2010-12-22
相关资源
最近更新 更多