Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从通过requests.get()创建的json中转义反斜杠_Python_Html_Beautifulsoup_Python Requests_Html Escape Characters - Fatal编程技术网

Python 从通过requests.get()创建的json中转义反斜杠

Python 从通过requests.get()创建的json中转义反斜杠,python,html,beautifulsoup,python-requests,html-escape-characters,Python,Html,Beautifulsoup,Python Requests,Html Escape Characters,我通过restapi url中的requests.get()收集数据,并修改该数据,然后通过requests.put()将修改后的数据再次推回到链接。问题是我通过request.put()发送的修改后的html代码具有反斜杠字符,这导致内部服务器错误状态代码为500。我将向您展示我使用requests.put()命令收集的部分html代码,其中包含反斜杠 <p><br /></p><table><colgroup><col /&g

我通过restapi url中的requests.get()收集数据,并修改该数据,然后通过requests.put()将修改后的数据再次推回到链接。问题是我通过request.put()发送的修改后的html代码具有反斜杠字符,这导致内部服务器错误状态代码为500。我将向您展示我使用requests.put()命令收集的部分html代码,其中包含反斜杠

<p><br /></p><table><colgroup><col /><col /><col /><col /><col /><col /></colgroup><tbody><tr><th><p>JIRA</p></th><th><p>Type</p></th><th><p>PR</p></th><th><p>Commit</p></th><th><p>Author</p></th><th><p>Date</p></th></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-5742\">IAPETUSZ-5742</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/9739\">9739</a></p></td><td><p>caf1229bb6a</p></td><td><p>ang</p></td><td><p>Thu, 4 Feb 2021 18:02:33 -0800</p></td></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-5991\">IAPETUSZ-5991</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/9971\">9971</a></p></td><td><p>db996d499cb</p></td><td><p>Mo</p></td><td><p>Fri, 19 Feb 2021 14:18:27 -0800</p></td></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-5971\">IAPETUSZ-5971</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/10067\">10067</a></p></td><td><p>216905485f1</p></td><td><p><br /></p></td><td><p>Fri, 26 Feb 2021 00:12:01 -0800</p></td></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-6200\">IAPETUSZ-6200</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/10126\">10126</a></p></td><td><p>ca28f68a0a5</p></td><td><p>Chowdhury</p></td><td><p>Tue, 9 Mar 2021 16:37:18 -0800</p></td></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-5608\">IAPETUSZ-5608</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/9510\">9510</a></p></td><td><p>b316a1c21be</p></td><td><p>Chowdhury</p></td><td><p><br /></p></td></tr><tr><td><p><a href=\"https://cejira.sandisk.com/browse/IAPETUSZ-5075\">IAPETUSZ-5075</a></p></td><td><p>Action-Item</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/8971\">8971</a></p></td><td><p>69fc6076c4f</p></td><td><p>ti</p></td><td><p>Fri, 11 Dec 2020 12:03:21 -0800</p></td></tr><tr><td><p><a href=\"https://cejira.ai.com/browse/IAPETUSZ-5757\">IAPETUSZ-5757</a></p></td><td><p>Bug</p></td><td><p><a href=\"https://bitbucket.ai.com/projects/FPGCSS/repos/cssh2p2/pull-requests/9666\">9666</a></p></td><td><p>dfe303b4260</p></td><td><p>rg</p></td><td><p>Fri, 29 Jan 2021 09:54:31 -0800</p></td></tr></tbody></table><p class=\"auto-cursor-target\"><br /></p><p><br /></p>

我认为JSON数据不喜欢“and so requests.put()显示状态代码500。

在所有代码中,您在哪里遇到并通过该URL?还记得不要混淆字符串内容和字符串表示之间的差异。如果您有一个包含
abc”的字符串def“ghi
,它将作为
“abc\”def“ghi打印“
。反斜杠不是字符串的一部分。它们只是印刷品中的代表。这一点一开始可能会让人非常困惑。@TimRoberts我认为操纵部分并不重要,所以我以前没有包括它,但现在我包括了。这并没有造成任何差别。我还将JSON内容即response['body']['storage']['value']直接从url复制到我的request.put()命令,但仍然会出现相同的错误,但如果我去掉反斜杠,它就可以工作。我应该如何避开请求的反斜杠。put()@TimRoberts我已将html代码更新为well@GarrGodfrey不,我不是。如何对JSON进行编码?我想一旦我从requests.get()收集了数据,它就已经被编码了
import requests
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup

response = requests.get('https://confluence.ai.com/rest/api/content/525424594?expand=body.storage',
                        auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@wdc')).json()

soup = BeautifulSoup(response['body']['storage']['value'], 'html.parser')

html = soup.contents

for idx in range(0, len(html)):
    new_tag = soup.new_tag("a", href="https://bitbucket.ai.com/projects//pull-requests/")
    if idx == 2:
        print("Contents of TABLE %%%%%%%%%%%%%%%%%%")
        print(html[idx])
        print('\n')
        # [br.extract() for br in html[idx].find_all('br')]
        for br in html[idx].find_all('br'):
            br.extract()
            html.insert(idx, new_tag)
            html[idx].string = "MY_Link"

pass_string = str(soup)

headers = {
    'Content-Type': 'application/json',
}

data = '{"id":"525424594","type":"page", "title":"Feature Automation - POST data","space":{"key":"CSSai"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":2}}'

response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@wdc'))

print(response.json())