收藏
回答

公众号新建草稿API, 正文(content)使用html,上传草稿后系统会自动加入 ?

根据:https://developers.weixin.qq.com/doc/offiaccount/Draft_Box/Add_draft.html

例如说content我简单设置成:

<p>line 1</p>

<p><br></p>

<p>line 2</p>

(line1和line2之间只空1行)

上传后其content html会变成:

<p>line 1</p>

<p>&nbsp;<br></p>

<p>line 2</p>

系统会自动加入&nbsp; 这样就变成line1和line2之间空了两行

请问如何设置,系统才不会自动加入&nbsp? 我需要的是我设置的content模板是什么,系统就上传什么。谢谢!

回答关注问题邀请回答
收藏

2 个回答

  • 社区技术运营专员--许涛
    社区技术运营专员--许涛
    08-27

    你好,请问具体哪里有添加nsbp

    08-27
    有用
    回复 6
    查看更多(1)
  • Total
    Total
    08-27

    你好 我是用壹伴的插件查看html, 如上图所示。不只是上面的html, 如果用其它的html我也会遇到类似的上传到草稿后系统帮我添加html的问题。

    以下是我的代码:

    #html content
    <p>line 1
    </p>
    <p>
      <br>
    </p>
    <p>line 2
    </p>
    _---------------------------------------------------------------------
    import requests
    import json
    import os
    
    # Set up environment
    os.environ['no_proxy'] = '*'
    
    # ====================Config==========================#
    # Personal access token and bot ID
    personal_access_token = 'xxx'
    bot_id = 'xxx'
    # Query string
    query = "1"
    # Number of times to repeat the request
    repeat_time = 3
    
    # Draft API URL Template (replace ACCESS_TOKEN with actual token)
    draft_api_url_template = "https://api.weixin.qq.com/cgi-bin/draft/add?access_token={access_token}"
    
    # html_file_path = r'D:\sjuvi\Documents\5.公众号开发\03.公众号正文图片\html_template\xiegang_template.html'
    html_file_path = r'C:\Users\sjuvi\Desktop\weixin_test\test2.html'
    
    # ====================Config==========================#
    
    # Read the content of the HTML file
    with open(html_file_path, 'r', encoding='utf-8') as file:
        html_content = file.read()
    
    # Article details with the extracted HTML content
    articles = [
        {
            "title": "中文标题",  # Chinese title
            "content": html_content,
            "thumb_media_id": "PdEzMawG529MXCtwub53gRYEoq6kuxaaQwa0HuVwrURiqiZTBjkpvrzlFEj4cfs2",
            # "pic_crop_235_1":"0_0_1_0.5236",
            "pic_crop_1_1":"0.04_0_0.4655_1",
        }
    ]
    
    # Define the URL and headers for the first API call
    url = 'https://api.coze.cn/open_api/v2/chat'
    headers = {
        'Authorization': f'Bearer {personal_access_token}',
        'Content-Type': 'application/json; charset=utf-8',  # Ensure UTF-8 encoding
        'Accept': '*/*',
        'Host': 'api.coze.cn',
        'Connection': 'keep-alive',
    }
    
    # Define the request payload
    data = {
        "conversation_id": "123",
        "bot_id": bot_id,
        "user": "123333333",
        "query": f'{query}',
        "stream": False
    }
    
    # Convert the payload to JSON with ensure_ascii=False to preserve Chinese characters
    json_data = json.dumps(data, ensure_ascii=False)
    
    # Send the POST request to retrieve the access code
    response = requests.post(url, headers=headers, data=json_data.encode('utf-8'))  # Encode in UTF-8
    
    # Check if the request was successful
    if response.status_code == 200:
        # Parse the response as JSON
        response_json = response.json()
    
        # Extract the 'content' field where the access code is nested
        content_str = response_json['messages'][0]['content']
    
        # Parse the nested JSON in the 'content' field
        content_json = json.loads(content_str)
    
        # Extract the access code from the 'output' field
        access_code = content_json['output']
    
        print(f'Extracted Access Code: {access_code}')
    
        # Now use the access_code to make the POST request to add the draft
        draft_api_url = draft_api_url_template.format(access_token=access_code)
    
        # Define the request payload for the draft API
        draft_payload = {
            "articles": articles
        }
    
        # Convert the draft payload to JSON with ensure_ascii=False to preserve Chinese characters
        draft_json_data = json.dumps(draft_payload, ensure_ascii=False)
    
        # Send the POST request to add the draft
        draft_response = requests.post(draft_api_url, headers=headers, data=draft_json_data.encode('utf-8'))
    
        if draft_response.status_code == 200:
            draft_response_json = draft_response.json()
            print(f'Draft API Response: {draft_response_json}')
        else:
            print(f'Failed to add draft. Status Code: {draft_response.status_code}')
    else:
        print(f'Failed to get a valid response for access code. Status Code: {response.status_code}')
    
    
    08-27
    有用
    回复
登录 后发表内容