html表单怎么填写,填写并提交html表单
除了⼤卫提到的Mechanize和Selenium之外,还可以使⽤Requests和BeautifulSoup来实现。
为了更清楚,使⽤Requests向服务器发送请求并从服务器检索响应,并使⽤BeautifulSoup解析响应html以了解要发送到服务器的参数。以下是我编写的⼀个⽰例脚本,它使⽤Requests和BeautifulSoup提交⽤户名和密码来登录:
import requests
from bs4 import BeautifulSoup as bs
def get_login_token(raw_resp):
soup = bs(, 'lxml')
token = [n['value'] for n in soup.find_all('input')
if n['name'] == 'wpLoginToken']
return token[0]
payload = {
'wpName': 'my_username',
'wpPassword': 'my_password',
'wpLoginAttempt': 'Log in',
#'wpLoginToken': '',
}
with requests.session() as s:
resp = s.get('/w/index.php?title=Special:UserLogin')
payload['wpLoginToken'] = get_login_token(resp)
response_post = s.post('/w/index.php?title=Special:UserLogin&action=submitlogin&type=login',
data=payload)
response = s.get('/wiki/Special:Watchlist')
更新:
对于您的具体情况,这是⼯作代码:
import requests
from bs4 import BeautifulSoup as bs
def get_session_id(raw_resp):
soup = bs(, 'lxml')
token = soup.find_all('input', {'name':'survey_session_id'})[0]['value']
return token
payload = {
'f213054909': 'o213118718', # 21st checkbox
'f213054910': 'Ronald', # first input-field
'f213054911': 'ronaldG54@gmail',
}
url = r'app.e2ma/app2/survey/39047/213008231/f2e46b57c8/?v=a' with requests.session() as s:
resp = s.get(url)html中提交表单用什么属性
payload['survey_session_id'] = get_session_id(resp)
response_post = s.post(url, data=payload)
print

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。