无法将请求发送到包含一个或多个请求的API

2024-04-24 00:55:38 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在尝试向API发送post请求,如下所示: https://www.cpaaustralia.com.au/api/FindACpa/SearchApi.mvc/ExecuteSearch

以下是JSON数据:

{"Point":{"Latitude":"-27.67361480","Longitude":"153.18297950"},"LocationText":"4000 Pacific Hwy, Loganholme QLD 4129, Australia","PostCode":"4129","Locality":"Loganholme","CountryCode":"AU"}

这是一个scrapy shell请求:

req=Request("https://www.cpaaustralia.com.au/api/FindACpa/SearchApi.mvc/ExecuteSearch",method="POST",headers={"user-agent":"Mozilla 5.0"},body=json.dumps(data))

此请求应在响应中返回JSON数据,但不会得到JSON响应。而是得到一个错误页面。你知道吗

请告诉我解决办法


Tags: 数据httpscomapijsonwwwpostau
1条回答
网友
1楼 · 发布于 2024-04-24 00:55:38

在对JSON发出POST请求时,您已经从Find a CPA页面中删除了__RequestVerificationToken,并更新了头中的__RequestVerificationToken。你知道吗

查找CPA页:https://www.cpaaustralia.com.au/FindACpa/Locate.mvc/Index

import bs4
import requests

# Create Session.
session = requests.session()

# Get HTML of Find a CPA Page.
find_a_cpa_page_url = 'https://www.cpaaustralia.com.au/FindACpa/Locate.mvc/Index'
find_a_cpa_page_response = session.get(find_a_cpa_page_url)
print('Response status code for Find a CPA Page:', find_a_cpa_page_response.status_code)

# Fetch Request Verification Token HTML.
soup = bs4.BeautifulSoup(find_a_cpa_page_response.text, 'lxml')
request_verification_token = soup.find('input', {'name': '__RequestVerificationToken'}).get('value')

url = 'https://www.cpaaustralia.com.au/api/FindACpa/SearchApi.mvc/ExecuteSearch'
json_data = {"Point":{"Latitude":"-27.67361480","Longitude":"153.18297950"},"LocationText":"4000 Pacific Hwy, Loganholme QLD 4129, Australia","PostCode":"4129","Locality":"Loganholme","CountryCode":"AU"}

# Add Request Verification Token in headers.
headers = {
    'Host': 'www.cpaaustralia.com.au',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31',
    '__RequestVerificationToken': request_verification_token,
    'Referer': 'https://www.cpaaustralia.com.au/FindACpa/Locate.mvc/Index',
}

response = session.post(url, json=json_data, headers=headers)
print('Response status code for JSON:', response.status_code);print('JSON Data:', response.content)

输出:

Response status code for Find a CPA Page: 200
Response status code for JSON: 200
JSON Data: b'{"ErrorMessage":"","Results":[{"AfsLicensee":"","CompanyName":"C & K Accounting Services (Qld) Pty Ltd","CpaType":"PP","DistanceKm":0.177551037039086,"EmailAddress":"sking@candkaccounting.com.au","Fax":"","FormattedAddress":"Unit 4B, , 16 \xe2\x80\x93 18 Beenleigh Redland Bay Road  , Loganholme  , Queensland 4129, AUSTRALIA","Languages":"","Point":{"Latitude":-27.673364,"Longitude":153.184757},"Postcode":"4129","PractitionerName":"","Telephone":"617 3219 7811","WebsiteAddress":""},{"AfsLicensee":"","CompanyName":"Dynamic Accountants Pty Ltd","CpaType":"PP","DistanceKm":0.594803802705306,"EmailAddress":"info@dynamicaccountants.com.au","Fax":"07 3801 4773","FormattedAddress":"Suite 1, 3950 Pacific Hwy, LOGANHOLME, Queensland 4129, AUSTRALIA","Languages":"","Point":{"Latitude":-27.669489,"Longitude":153.179123},"Postcode":"4129","PractitionerName":"","Telephone":"07 3801 9555","WebsiteAddress":""},{"AfsLicensee":"","CompanyName":"Business Accounting Specialists","CpaType":"PP","DistanceKm":1.15412476831002,"EmailAddress":"nick@businessaccountingspecialists.com.au","Fax":"","FormattedAddress":"The Groves, Suite 11, 3986 Pacific Highway, LOGANHOLME, Queensland 4129, AUSTRALIA","Languages":"","Point":{"Latitude":-27.683700,"Longitude":153.185900},"Postcode":"4129","PractitionerName":"","Telephone":"07 3801 9555","WebsiteAddress":"http:\\/\\/www.businessaccountingspecialists.com.au"},{"AfsLicensee":"","CompanyName":"BMT Accounting &  Tax Pty Ltd","CpaType":"PP","DistanceKm":1.32102938225839,"EmailAddress":"info@bmtaccounting.com.au","Fax":"","FormattedAddress":"Suite 36, 37-59 Bryants Road, Loganholme, Queensland 4129, AUSTRALIA","Languages":"","Point":{"Latitude":-27.663443,"Longitude":153.175997},"Postcode":"4129","PractitionerName":"","Telephone":"07 3333 2350","WebsiteAddress":"http:\\/\\/www.bmtaccounting.com.au"},{"AfsLicensee":"","CompanyName":"KEF Accounting","CpaType":"PP","DistanceKm":2.86625470700257,"EmailAddress":"kef@kefaccounting.com.au","Fax":"","FormattedAddress":"5\\/66 Commercial Drive, SHAILER PARK, Queensland 4128, AUSTRALIA","Languages":"","Point":{"Latitude":-27.651880,"Longitude":153.167231},"Postcode":"4128","PractitionerName":"","Telephone":"07 3806 4920","WebsiteAddress":"http:\\/\\/www.kefaccounting.com.au"}]}'

相关问题 更多 >