我正在尝试使用python中的requests模块读取网页,使用beautifulsoup抓取读取的数据,修改汤对象中的数据,并使用request.put()方法将其写回到同一网页。 / p>
在汤对象中还有什么要做的事情,以将其分配给JSON有效负载?
用于此目的的模块: 1. JSON 2.要求 3. BeautifulSoup
如果我正在准备的JSON有效负载中没有汤对象,那么request.put()会正确写入数据
response = requests.get(confluenceServerURL+getPageURL, verify=False, headers=headers)
# 200 OK is the response for the web page is accessible
if response.status_code == 200:
jsonD = response.json()
pageId = jsonD["results"][0]['id']
versionNum = jsonD["results"][0]['version']['number']
body = jsonD["results"][0]['body']['storage']['value']
soup = BeautifulSoup(response.content, 'html.parser')
input_str = "test string"
testMan = "found string"
for row in soup.find_all('tr'):
for node in row.find_all('td'):
if node.text == testMan:
node.string = input_str
newBody = soup
else: #response.status_code
logger.error("Error while obtaining page with given page title %s ", response.status_code)
exit(1);
## Space to write confluence page
logger.info('--------------Updating page--------------')
updatePageURL = "/rest/api/content/"+pageId
jsonPayload = {"id":pageId,"type":"page","title":pageTitle,"space":{"key":spaceKey},
"body":{"storage":{"value":"{0}".format(newBody),"representation":"storage"}},
"version":{"number":int(versionNum)+1}}
#print(jsonPayload)
# update page
response = requests.put(confluenceServerURL+updatePageURL,verify=False,data=json.dumps(jsonPayload),headers=headers)
logger.debug('URL for updating page is %s', response.url)
if response.status_code == 200:
logger.info("Page with id %s updated successfully",pageId)
#break;
else:
logger.error("Unable to update page with id %s",pageId)
logger.error(response.text)
我获得request.put()的响应代码为200,但是如果正在使用beautifulsoup,则页面将保持空白。如果直接采用JSON格式,则说明页面已成功更新
INFO:conf_rest_get_page:--------------获取页面------------------- DEBUG:conf_rest_get_page:从URL获取SMSESSION 无障碍 DEBUG:conf_rest_get_page:用于获得融合页面的URL是URL
INFO:conf_rest_get_page:--------------更新页面---------------- INFO:conf_rest_get_page:ID为261042277的页面已成功更新
2019-06-09 10.26.58 =====表过程已结束,状态:True =====