我不熟悉Python和
我想搜索是否选中了广播框。
这是我的HTML代码。
<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 :(得分:1)
尝试has_attr
:
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