Python 如何在requests.put()中自动更新网页版本?

Python 如何在requests.put()中自动更新网页版本?,python,json,beautifulsoup,httprequest,python-requests-html,Python,Json,Beautifulsoup,Httprequest,Python Requests Html,我正在脚本中使用requests.put()方法自动更新网页。问题在于,自动化脚本并没有完全自动化。让我向您展示导致问题的代码片段: import requests import json from requests.auth import HTTPBasicAuth from bs4 import BeautifulSoup headers = { 'Content-Type': 'application/json', } # Just a stri

我正在脚本中使用requests.put()方法自动更新网页。问题在于,自动化脚本并没有完全自动化。让我向您展示导致问题的代码片段:

import requests
import json
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup

headers = {
        'Content-Type': 'application/json',
    }
    
    # Just a string payload being extracted from previous lines of code not shown here
    pass_string = str(soup).replace('\"', '\\"')

    data = '{"id":"525424594","type":"page", "title":"Update status","space":{"key":"CSSAI"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":44}}'


    response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data,
                            auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
因此,在名为data的JSON字符串中,我们有一个名为“version”:{“number”:44}的键,用于更新网页,这样我们就不会在页面版本方面有任何冲突。而且,它应该在网页内容每次更改时更新。 有两种情况会改变网页的“版本”:

  • 一旦我发送了我的请求。put()http请求,页面就会更新,版本必须增加1
  • 如果有人通过转到网页链接本身来更新网页,并手动更改该网页的内容,则版本也会更新
  • 对于案例1,我可以有一个.txt文件,该文件将记录网页的早期版本,这样每次执行脚本时,我都可以从.txt文件中读取早期版本,并且该版本会在脚本中自动递增1,将该版本写入.txt文件,并使用递增的版本执行命令。 但对于案例2,我不知道是否有人更改了网页本身的版本,因此很难知道该网页的当前版本。
    关于如何解决这个问题,有什么想法吗?

    经过进一步思考,我找到了所需的解决方案。如果有人有更好的解决方案,请张贴在这里

    import requests
    import json
    from requests.auth import HTTPBasicAuth
    from bs4 import BeautifulSoup
    
    headers = {
            'Content-Type': 'application/json',
        }
        
        # Just a string payload being extracted from previous lines of code not shown here
        pass_string = str(soup).replace('\"', '\\"')
    
        data = '{"id":"525424594","type":"page", "title":"Update status","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@ai'))
    
        if response.json()["statusCode"] == 409:
            error_message = "Version must be incremented on update. Current version is: "
            if error_message in response.json()["message"]:
                current_version = response.json()["message"].split(error_message)[1]
                version_num = int(current_version) + 1
                data = '{"id":"525424594","type":"page", "title":"Update Status","space":{"key":"CSSAI"},"body":{"storage":{"value":"' + pass_string + '","representation":"storage"}}, "version":{"number":' + str(
                    version_num) + '}}'
                response = requests.put('https://confluence.ai.com/rest/api/content/525424594', headers=headers, data=data,
                                        auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
        else:
            print(response.json())
            sys.exit(1)
    

    基本上,我从失败的响应中收集当前版本号,并将其递增1以发送新请求。但是,这确实意味着我们的第一个请求将始终是失败的请求。

    您所说的“更改网站本身的版本”是什么意思?@Anonymous我的意思是在confluence网页中,我们有一个名为“编辑”的选项,用户可以单击“编辑”并通过键入或附加内容来更改某些内容。您不能让该按钮发出PUT请求吗?@Anonymous您是什么意思?编辑按钮如何发出PUT请求?PUT请求应该在脚本中进行。编辑按钮位于网页中。编辑按钮可触发网站代码以发出PUT请求。