python—将值插入到列表外空白处的html表中

hmmo2u0o  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(213)

我在一个网站中有一个空表,我想用我的脚本自动填充这个表。该表如下链接所示:table
当我转到页面的rest/api/content链接时,我会看到一个页面,它以json的形式显示内容,比如:

{"id":"525424594","type":"page","status":"current","title":"Feature Automation - POST data","body":{"storage":{"value":"<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-Num</p></th><th><p>Commit</p></th><th><p>Author</p></th><th><p>Date</p></th></tr><tr><td><p>Cloud-5742</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5991</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5971</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-6200</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5608</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr><tr><td><p>Cloud-5075</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5757</p></td><td><p>Bug</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr></tbody></table><p class=\"auto-cursor-target\"><br /></p>","representation":"storage","_expandable":{"content":"/rest/api/content/525424594"}},"_expandable":{"editor":"","view":"","export_view":"","styled_view":"","anonymous_export_view":""}},"extensions":{"position":"none"},"_links":{"webui":"/display/CloudFW/Feature+Automation+-+POST+data","edit":"/pages/resumedraft.action?draftId=525424594&draftShareId=0984750c-3696-4918-b9ce-4358fefc26c9","tinyui":"/x/0ldRHw","collection":"/rest/api/content","base":"https://confluence.cloudai.com","context":"","self":"https://confluence.cloudai.com/rest/api/content/525424594"},"_expandable":{"container":"/rest/api/space/CloudFW","metadata":"","operations":"","children":"/rest/api/content/525424594/child","restrictions":"/rest/api/content/525424594/restriction/byOperation","history":"/rest/api/content/525424594/history","ancestors":"","version":"","descendants":"/rest/api/content/525424594/descendant","space":"/rest/api/space/CloudFW"}}

然后,我使用beautifulsoup提取存储值以进行修改,并在空白处插入列表中的值。提取存储值后,我得到如下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-Num</p></th><th><p>Commit</p></th><th><p>Author</p></th><th><p>Date</p></th></tr><tr><td><p>Cloud-5742</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5991</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5971</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-6200</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5608</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr><tr><td><p>Cloud-5075</p></td><td><p>Action-Item</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><p><br /></p></td></tr><tr><td><p>Cloud-5757</p></td><td><p>Bug</p></td><td><p><br /></p></td><td><p><br /></p></td><td><br /></td><td><br /></td></tr></tbody></table><p class=\"auto-cursor-target\"><br /></p>

我拥有这样的列表,每个列表的内容都是按照表行的时间顺序排列的。例如,我有一个名为commit的列表。它有7个值,应该按顺序插入到表的空格中,这样列表中第一个索引中的元素将进入第一个空格,第二个索引元素进入第二个空格,依此类推。我很难弄清楚如何将列表中的元素插入到存储值的空格中。这是我当前的代码,我试图提取/删除标记,我假设这些标记是用来代替空格的标记,并插入我自己的标记“my values”和列表中的my value。

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

response = requests.get('https://confluence.cloudai.com/rest/api/content/525424594?expand=body.storage',
                        auth=HTTPBasicAuth('ServiceAccount_Cloud@cloudai.com', 'Cloud123')).json()

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

html = soup.contents

# lists where the values going inside the blanks is saved pr_num, commit, date and author. Not shown here

# as not sure how to insert them in which order

for idx in range(0, len(html)):
    new_tag = soup.new_tag("a", pr_num[idx])
    # 2nd index of html list contains the html code of the whole table.
    # Parsing the table to get rid of the blank tags br and inserting my own value in the blank
    if idx == 2:
        print("Contents of TABLE %%%%%%%%%%%%%%%%%%")
        for br in html[idx].find_all('br'):
            br.extract()
            html.insert(idx, new_tag)

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

string_insert = str(soup)
data = '{"id":"525424594","type":"page", "title":"Feature Automation - POST data","space":{"key":"CloudFw"},"body":{"storage":{"value":"' + string_insert + '","representation":"storage"}}, "version":{"number":2}}'
response = requests.put('https://confluence.cloudai.com/rest/api/content/525424594', headers=headers, data=data, auth=HTTPBasicAuth('ServiceAccount_Cloud@cloudai.com', 'Cloud123'))

print(response.json())

我是python请求模块的新手,所以我不知道如何在正确的空格中插入所有元素。我也没有正确理解html代码,不知道哪个构成哪个空格,以及如何正确地在文本中插入数据 requests.put 命令,这样我就不会得到任何错误和变化得到反映在网站上。
注意:网站链接和密码是假的。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题