我创建了一个脚本,使用 Python 和 requests 模块,根据此网页中的许可证号获取电话号码。该脚本应该生成一个 JSON 响应,其中包含我感兴趣的电话号码。
当我在搜索框中手动输入许可证号 354206 并点击搜索按钮时,它会生成一个包含电话号码的结果。我正在尝试创建脚本,以便它会产生相同的响应结果。但是,我得到了以下回复。
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}
这是我用来尝试获得所需结果的脚本:
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())
如何使用许可证号码和请求模块从该网页获取电话号码?
你可以尝试:
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"])
打印(已编辑以不显示完整数量):
354206 9165951XXX
354207 (520) 391-0XXX