在BeautifulSoup / Python中选择具有特定属性的标签

时间:2015-01-13 11:57:16

标签: python html parsing python-2.7 beautifulsoup

import os
from bs4 import BeautifulSoup

do = dir_with_original_files = 'C:\Users\ADMIN\Desktop\\new_folder'
dm = dir_with_modified_files = 'C:\Users\ADMIN\Desktop\\new_folder\\test'

for root, dirs, files in os.walk(do):
    for f in files:
        print f.title()
        if f.endswith('~'): #you don't want to process backups
            continue
        original_file = os.path.join(root, f)
        mf = f.split('.')
        mf = ''.join(mf[:-1])+'_mod.'+mf[-1] # you can keep the same name 
                                             # if you omit the last two lines.
                                             # They are in separate directories
                                             # anyway. In that case, mf = f
        modified_file = os.path.join(dm, mf)
        with open(original_file, 'r') as orig_f, \
            open(modified_file, 'w') as modi_f:
            soup = BeautifulSoup(orig_f.read())

            for t in soup.find_all('td', class_='findThisClass'):
                for child in t.find_all("font"):
                    if child.string is not None:
                        child.string.wrap(soup.new_tag('h2'))
            for t in soup.find_all('table', class_='tableClass'):
                t.extract()
            # This is where you create your new modified file.
            modi_f.write(soup.prettify().encode(soup.original_encoding)) 

此代码将在类<font>中找到所有<td class=findThisClass>个标记,并在这些字体标记中添加。

我想要做的是找到带有此标记的所有html:

<font color="#333333" face="Verdana" size="3" style="font-weight: bold; background-color: rgb(255, 255, 255);">

如果出现以下情况,最好的方法是:

(a)我确信字体将始终遵循相同的形式(所有属性的顺序相同,ctrl + f与此字符串将找到我想要的所有匹配项):

<font color="#333333" face="Verdana" size="3" style="font-weight: bold; background-color: rgb(255, 255, 255);">

(b)如果我想让它工作,即使切换属性顺序,例如:

<font color="#333333" face="Verdana" size="3" style="font-weight: bold; background-color: rgb(255, 255, 255);">

但也要改变

<font face="Verdana" color="#333333" size="3" style="font-weight: bold; background-color: rgb(255, 255, 255);">

非常感谢。

1 个答案:

答案 0 :(得分:2)

attrs字典提供特定值:

t.find_all("font", attrs={'face': 'Verdana', 'color': '#333333', 'size': '3', 'style': 'font-weight: bold; background-color: rgb(255, 255, 255);'})
相关问题