我对python还是个新手,边学边学,所以请容忍我..如何让bs4从所提供的代码中只获取“value=* number *”数据?
<div class="option-radios">
<label class="radio option-7">
<em style="display: none"></em>
<span class="radio-label">7</span>
<input id="option-7"
type="radio" name="option[1]"
class=" property-uid1"
value="59" title="7" />
</label>
<label class="radio option-8">
<em style="display: none"></em>
<span class="radio-label">8</span>
<input id="option-8"
type="radio" name="option[1]"
class=" property-uid1"
value="61" title="8" />
</label>
<label class="radio option-9">
<em style="display: none"></em>
<span class="radio-label">9</span>
<input id="option-9"
type="radio" name="option[1]"
class=" property-uid1"
value="62" title="9" />
</label>
<label class="radio option-10">
<em style="display: none"></em>
<span class="radio-label">10</span>
<input id="option-10"
type="radio" name="option[1]"
class=" property-uid1"
value="63" checked="checked" title="10" />
</label>
<label class="radio option-11">
<em style="display: none"></em>
<span class="radio-label">11</span>
<input id="option-11"
type="radio" name="option[1]"
class=" property-uid1"
value="60" title="11" />
</label>
我可以获得div中的所有数据,但我无法将搜索范围缩小到我想要的范围。
import requests
from bs4 import BeautifulSoup as bs
session = requests.session()
url = "url here"
a = session.get(url)
soup = bs(a.text,'html.parser')
sizes = soup.find('div',{'class':'option-radios'})
print(sizes)再次为可能是一个非常基本的问题道歉!
发布于 2020-05-30 19:16:57
在这种情况下,使用css选择器可能会更好:
for val in soup.select('label input'):
print(val['value'])输出(针对问题中的代码片段):
59
61
62
63
60https://stackoverflow.com/questions/62101478
复制相似问题