使用 python 从 Postman 中提取正文内容

Extract content of body from Postman using python

我正在尝试在收到 Postman 的回复后从正文中检索特定代码。 我想检索此 ID:00163E7B0F671EDA82E31CA5B621A4B3 并将其写入 csv 文件

正文内容如下:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://my344540.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
    <id>https://my344540.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountCollection</id>
    <title type="text">CorporateAccountCollection</title>
    <updated>2022-03-15T08:36:52Z</updated>
    <author>
        <name/>
    </author>
    <link href="CorporateAccountCollection" rel="self" title="CorporateAccountCollection"/>
    <entry m:etag="W/&quot;datetimeoffset'2020-02-06T12%3A39%3A34.1278380Z'&quot;">
        <id>https://my344540.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountCollection('00163E7AF1111EEA82DBB6D559A072EF')</id>
        <title type="text">CorporateAccountCollection('00163E7AF1111EEA82DBB6D559A072EF')</title>
        <updated>2022-03-15T08:36:52Z</updated>
        <category term="c4codata.CorporateAccount" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
        <link href="CorporateAccountCollection('00163E7AF1111EEA82DBB6D559A072EF')" rel="edit" title="CorporateAccount"/>
        <content type="application/xml">
        </content>
    </entry>
</feed>

ID多处重复但相同。请帮忙

要发出 HTTP 请求,请使用 Python 的 requests 库。

要解析 XML 响应,请使用 built-in xml.etree 库。要查询<id>标签,可以使用XPath.

请求示例:

import requests

r = requests.get('https://api.github.com/events')
print(r.text)  # parse your response with xml parser

完整示例:

import xml.etree.ElementTree as ET
import csv
import requests

response = requests.get('https://example.com/your_path')
root: ET.Element = ET.fromstring(response.text)

ids = [id_element.text.strip() for id_element in root.findall('.//id')]

with open('output.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow(['id'])
    for id in ids:
        writer.writerow([id])

您可以使用 requests to download the XML content. The URL you provided requires a username and password using basic HTTP authentication, so you have to supply those with the request. The documentation for doing that with the requests library is here.

要解析 XML,您可以使用 xml.etree.elementtree。因为XML文档有一个默认命名空间(feed节点的xmlns属性),所以在指定元素时需要指定默认命名空间。例如,entry 元素需要指定为 {http://www.w3.org/2005/Atom}entry.

然后,您可以使用re module to parse the ID from the resulting URL using regular expressions. And finally, you can use the csv模块将生成的ID写入CSV文件。

我在下面包含了一些代码来执行此操作。当您 运行 它时,您应该将“foo”和“bar”替换为您提供的 URL 的实际用户名和密码。

import csv
import re
import xml.etree.ElementTree as ET

import requests
from requests.auth import HTTPBasicAuth

URL = "https://my344540.crm.ondemand.com/sap/c4c/odata/v1/c4codataapi/CorporateAccountCollection?$filter=AccountId%20eq%20%27200148859"
USERNAME = "foo"
PASSWORD = "bar"
XMLNS = "http://www.w3.org/2005/Atom"

def fetch_xml(url, username, password):
    response = requests.get(url, auth=HTTPBasicAuth(username, password))
    response.raise_for_status()
    return response.text

def parse_xml(xml_text):
    root = ET.fromstring(xml_text)
    id_element = root.find(f"./{{{XMLNS}}}entry/{{{XMLNS}}}id")
    return id_element.text

def parse_url(url):
    match = re.search("'([0-9A-F]+)'", url)
    if not match:
        raise ValueError(f"Could not parse ID from URL {url}")
    return match.group(1)

def write_csv(path, collection_id):
    with open(path, "w", encoding="utf-8", newline="") as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(["collection_id"])
        writer.writerow([collection_id])

def main():
    xml_text = fetch_xml(URL, USERNAME, PASSWORD)
    url = parse_xml(xml_text)
    collection_id = parse_url(url)
    print(collection_id)
    write_csv("result.csv", collection_id)

if __name__ == "__main__":
    main()

此外,在我的代码中我有 hard-coded 密码,但这是不安全的 - 最好是 store it in an environment variable