2017-07-10 10 views
0

私はPythonに慣れていません。Python:どのようにhtmlラジオボックスをチェックするのですか?

ラジオボックスがオンになっているかどうかを調べたいと思います。

ここは自分のHTMLコードです。 has_attrで試してみてください

<input type="radio" name="radio1" value="1" checked> 
<input type="radio" name="radio2" value="2"> 
<input type="radio" name="radio3" value="3" checked> 

が、私はこのコードを試してみたが、それは

import requests 
from requests.auth import HTTPBasicAuth 

from bs4 import BeautifulSoup 

res = requests.get('url', auth=HTTPBasicAuth('User', 'Password')) 

soup = BeautifulSoup(res.text,'html.parser') 
print soup.find(attrs={'name':'radio1'}).attrs['checked'] 
print soup.find(attrs={'name':'radio2'}).attrs['checked'] 
print soup.find(attrs={'name':'radio3'}).attrs['checked'] 
+0

https://stackoverflow.com/questions/27435600/determine-which-radiobutton-been-sel ected:これをチェックする –

答えて

0

動作しません:

from bs4 import BeautifulSoup 

div_test = """ 
<input type="radio" name="radio1" value="1" checked/> 
<input type="radio" name="radio2" value="2"/> 
<input type="radio" name="radio3" value="3" checked/> 
""" 

soup = BeautifulSoup(div_test,'html.parser') 
print soup.find('input',attrs={'name':'radio1'}).has_attr('checked') 
print soup.find('input',attrs={'name':'radio2'}).has_attr('checked') 
print soup.find('input',attrs={'name':'radio3'}).has_attr('checked') 

出力:

True 
False 
True 
関連する問題