在python中更改HTML表单元素

时间:2019-05-29 00:33:12

标签: python python-3.x

我想使用python而不是django将html表单动作替换为另一个文本

我尝试过

import requests
import re
main_input = input()
get_input = requests.get(main_input).read()
get_inputtxt = get_input.text
re_pattern = re.search('^action.*"$', get_inputtxt)
open_file = open('hello.htm', 'w')
print(get_inputtxt, file=open_file)
if (re_pattern):
  re_pattern = re_pattern.replace(re_pattern, "action='hello.php'")
  open_file.close()


normal action = "www.me.html"
expected action = "hello.php"

1 个答案:

答案 0 :(得分:0)

以下是使用BeautifulSoup的方法:

from bs4 import BeautifulSoup
HTML = """
<form id="some_id" action="www.me.html" method="post">
   <input name="nothing" value="">
</form>
"""
soup = BeautifulSoup(HTML, 'html.parser')
print(soup)
<form action="www.me.html" id="some_id" method="post"> 
<input name="nothing" value=""/> </form>
for form in soup('form'):
    print(form)
    form['action'] = "hello.php"

print(soup)
<form action="hello.php" id="some_id" method="post"> 
<input name="nothing" value=""/> </form>

您只需要添加url请求:

import requests
main_input = input()
HTML = requests.get(main_input).text