Created
March 28, 2024 02:29
-
-
Save xx10n31y/ff1ebabc19bbcb0bc1a62b3a233bb477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def eori(eori_number): | |
# 构建符合SOAP协议的XML消息 | |
soap_request = f""" | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://eori.ws.eos.dds.s/"> | |
<SOAP-ENV:Body> | |
<ns1:validateEORI> | |
<ns1:eori>{eori_number}</ns1:eori> | |
</ns1:validateEORI> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> | |
""" | |
# 设置请求头和URL | |
url = 'https://ec.europa.eu/taxation_customs/dds2/eos/validation/services/validation?wsdl' | |
headers = { | |
'Content-Type': 'text/xml', | |
} | |
# 发送POST请求并获取响应 | |
# , proxies=self.proxies | |
proxy = { | |
# input proxy | |
} | |
response = requests.post(url, data=soap_request, headers=headers,verify=False, proxies=proxy) | |
xml_body = response.text | |
import re | |
# 删掉 <?xml version='1.0' encoding='UTF-8'?> | |
xml_body = re.sub(r'<\?xml.*?\?>', '', xml_body) | |
# 删掉 <S:Envelope .* > | |
xml_body = re.sub(r'</?s:envelope(?:.*?)?>', '', xml_body, 0, re.IGNORECASE) | |
# 删掉 <S:body> | |
xml_body = re.sub(r'</?s:body>', '', xml_body, 0, re.IGNORECASE) | |
# 删掉 <ns0:validateEORIResponse.*> | |
xml_body = re.sub(r'</?ns0:validateEORIResponse(.*?)?>', '', xml_body, 0, re.IGNORECASE) | |
# 删掉 <return> | |
xml_body = re.sub(r'</?return>', '', xml_body, 0, re.IGNORECASE) | |
# 剩下的就是返回的数据了 | |
result_field_list = ['requestDate', 'eori', 'status', 'statusDescr', 'name', 'address', 'street', 'postalCode', | |
'city', 'country'] | |
result_field_key = { | |
'eori': 'eori_number', | |
'requestDate': 'request_date', | |
'status': 'is_valid', | |
'statusDescr': 'status_descr', | |
'name': 'company_name_en', | |
'address': 'company_address_en', | |
'street': 'company_street_en', | |
'postalCode': 'company_zipcode', | |
'city': 'company_city_en', | |
'country': 'company_country_en', | |
} | |
result = {} | |
for field in result_field_list: | |
match_result = re.findall(rf'<{field}>(.*?)</{field}>', xml_body, re.IGNORECASE | re.MULTILINE) | |
text = '' | |
if len(match_result): | |
text = match_result[0] | |
if field == 'status': | |
text = bool(int(text) - 1) | |
if field == 'postalCode': | |
result['company_zip_code'] = text | |
result[result_field_key[field]] = text | |
return result | |
if __name__ == '__main__': | |
eori_number = 'FRCN900276224' | |
eori() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment