Python:如何在POST请求中发送HTML代码
我想把HTML代码从Python脚本发送到Joomla网站。
description = "<h1>Header</h1><p>text</p>"
values = {'description' : description.encode(self.encoding),
'id = ' : 5,
}
data = urlencode(values)
binData = data.encode(self.encoding)
headers = { 'User-Agent' : self.userAgent,
'X-Requested-With' : 'XMLHttpRequest'}
req = urllib2.Request(self.addr, binData, headers)
response = urllib2.urlopen(req)
rawreply = response.read()
在Joomla服务器上,我得到了相同的字符串,但没有HTML:
$desc = JRequest::getVar('description', '', 'POST');
这是怎么回事呢?
2 个回答
2
你应该使用requests库
pip install requests
然后
import requests
description = "<h1>Header</h1><p>text</p>"
values = dict(description='description',id=5)
response = requests.post(self.addr,data=values)
if response.ok:
print response.json()
或者如果joomla没有返回json格式的数据
print response.content
1
JRequest::getVar 或 JRequest::getString 这两个方法会过滤掉 HTML 代码。不过,你可以选择关闭这个过滤功能:
$desc = JRequest::getVar('description', '', 'POST', 'string', JREQUEST_ALLOWHTML);