输出里面的字符串值
<td headers="mb_list_id" class="" style="text-align:center;font-weight:bold;">需要输出的字符串</td>
代码
匹配出以上td之后
td.string td.text
——————————-
匹配出带有不固定值的属性
<tr class="bg0"> ..... </tr> <tr class="bg1"> ..... </tr> <tr class="bg3"> ..... </tr>
代码
soup.find_all(name='tr', attrs={'class': True}) soup.find_all(name='tr', attrs={'class': re.compile("bg")})
—————————————–
提取出一大段中需要的内容
<div class="info"> <span class="zf10">无关的文字1</span> <span class="data fg10"> <i class="fa fa-clock-o"></i> 无关的文字2</span> 需要的内容 </div>
代码
soup = BeautifulSoup(ct, 'html.parser') print(soup.find(name='div', attrs={'class': 'info'}).contents[4].strip())
———————————
取出标签元素值
<td><select id="flag_stat25680" class="fs" seq="25680"><option value='10' selected>xxxx</option> <option value='15' >xx1</option> <option value='20' >xx2</option> <option value='30' >xx3</option> </select></td>
取出 seq的值
soup = BeautifulSoup(ct, 'html.parser') print(soup.select_one('select.fs').get('seq'))