pythonpython-3.xweb-scrapingpython-requests

Failed to produce a JSON response containing a phone number based on a license number from a webpage using the requests module


I've created a script to fetch a phone number based on a license number from this webpage, using Python with the requests module. The script is supposed to produce a JSON response containing the phone number I'm interested in.

When I manually input this license number 354206 in the search box and hit the search button, it produces a result with a phone number in it. I'm trying to create the script so that it will produce the same result in response. However, I get the following response instead.

200
{'event': {'descriptor': 'markup://aura:invalidSession', 'attributes': {'values': {}}, 'eventDef': {'descriptor': 'markup://aura:invalidSession', 't': 'APPLICATION', 'xs': 'I', 'a': {'newToken': ['newToken', 'aura://String', 'I', False]}}}, 'exceptionMessage': 'Guest user access is not allowed', 'exceptionEvent': True}

This is the script I'm using to try to get the desired results:

import requests
import json

link = 'https://azroc.my.site.com/AZRoc/s/sfsites/aura'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36',
    'Referer': 'https://azroc.my.site.com/AZRoc/s/contractor-search',
    'Origin': 'https://azroc.my.site.com',
    'Accept': '*/*',
    'Accept-Encoding': 'gzip, deflate, br, zstd',
    'Accept-Language': 'en-US,en;q=0.9',
    'X-Sfdc-Page-Scope-Id': 'dd1f6a24-3e7b-41e9-8fcb-f0bda9979cc2',
    'X-Sfdc-Request-Id': '13815039000012b044',
}

params = {
    'r': '5',
    'other.ARCP_ContractorSearch.getRecords': '1'
}
payload = {
    'message': '{"actions":[{"id":"176;a","descriptor":"apex://ARCP_ContractorSearch/ACTION$getRecords","callingDescriptor":"markup://c:ARCP_ContractorSearch","params":{"searchKey":"354206","classification":null,"city":""}}]}',
    'aura.context': '{"mode":"PROD","fwuid":"eGx3MHlRT1lEMUpQaWVxbGRUM1h0Z2hZX25NdHFVdGpDN3BnWlROY1ZGT3cyNTAuOC40LTYuNC41","app":"siteforce:communityApp","loaded":{"APPLICATION@markup://siteforce:communityApp":"wi0I2YUoyrm6Lo80fhxdzA","MODULE@markup://lightning:f6Controller":"5PtsAUCMnPdpZDcNTHXtbg","COMPONENT@markup://instrumentation:o11ySecondaryLoader":"1JitVv-ZC5qlK6HkuofJqQ"},"dn":[],"globals":{},"uad":false}',
    'aura.pageURI': '/AZRoc/s/contractor-search',
    'aura.token': 'null',
}

with requests.Session() as session:
    session.headers.update(headers)
    res = session.post(link,params=params,data=json.dumps(payload))
    print(res.status_code)
    print(res.json())

How can I get the phone number from that webpage using the license number with the requests module?


Solution

  • You can try:

    import json
    import requests
    
    url = "https://azroc.my.site.com/AZRoc/s/sfsites/aura?r=5&other.ARCP_ContractorSearch.getRecords=1"
    
    message = {
        "actions": [
            {
                "id": "176;a",
                "descriptor": "apex://ARCP_ContractorSearch/ACTION$getRecords",
                "callingDescriptor": "markup://c:ARCP_ContractorSearch",
                "params": {"searchKey": "<ID>", "classification": None, "city": ""},
            }
        ]
    }
    
    data = {
        "message": None,
        "aura.context": r'{"mode":"PROD","fwuid":"eGx3MHlRT1lEMUpQaWVxbGRUM1h0Z2hZX25NdHFVdGpDN3BnWlROY1ZGT3cyNTAuOC40LTYuNC41","app":"siteforce:communityApp","loaded":{"APPLICATION@markup://siteforce:communityApp":"wi0I2YUoyrm6Lo80fhxdzA","MODULE@markup://lightning:f6Controller":"5PtsAUCMnPdpZDcNTHXtbg","COMPONENT@markup://instrumentation:o11ySecondaryLoader":"1JitVv-ZC5qlK6HkuofJqQ"},"dn":[],"globals":{},"uad":false}',
        "aura.pageURI": "/AZRoc/s/contractor-search",
        "aura.token": "null",
    }
    
    to_search = ["354206", "354207"]
    
    for s in to_search:
        message["actions"][0]["params"]["searchKey"] = s
        data["message"] = json.dumps(message)
        r = requests.post(url, data=data).json()
        print(s, r["actions"][0]["returnValue"][0]["phone"])
    

    Prints (edited to not show full number):

    354206 9165951XXX
    354207 (520) 391-0XXX