正则表达式处理可能发生一次或两次的短语

时间:2012-12-10 04:00:19

标签: python regex

我真的想不出一个体面的标题来概述我正在尝试做什么,但我的例子应该很好地解释,我的公司提供在线时间表,但他们没有任何API或任何提取它的东西,所以我使用Python框架Scrapy来抓取数据,然后将其添加到我的Google日历

一个女孩给了我一个Regex系列来处理这些数据,因为它踢了我的屁股好几天,她感觉很好,但我已经意识到它不处理分班(很可能是因为我没有安排因为她没有看到一个人的可能性

我的正则表达式是

re.findall("""dow1'>(\w+)<\S+?>(\w+ \d+)</td>\s*<td class.*?tlHours'>(\d+).*?span>\s*(\d+)<span.*?ment'>(.*?)</spa.*?Meal: (.*?)</sp.*?start'>(\S+?)</spa.*?end'>(\S+?)<""", response.body)

示例数据:

这是正常的8小时一天用餐休息,处理得很好:

<tr>
    <td class='dt'>
        <span class='dow1'>Sunday</span>Dec 09
    </td>
    <td class='ScheduledDetails'valign='top'>
        <div style="position:relative;">
            <span class='tlHours'>8<span class='spart'> hrs</span> 0<span class='spart'> mins</span></span><span class='department'>Cashier</span><span class='meal'>Meal: 2pm - 3pm</span>
        </div>
    </td>
    <td>
        &nbsp;
    </td>
    <td class='Schedunderlay'>
        <div class='Sched'>
            <div class='schedbar' style='left: 143px; width: 234px;'>
                <div class='schedbar_l'></div>
                <div class='schedbar_m' style='width: 226px;'>
                    <span class='start'>10am</span><span class='end'>7pm</span>
                </div>
                <div class='schedbar_r'></div>
            </div>
            <div class='availbar' style='left: 9px; width: 498px; display: none;'>
                <div class='schedbar_l'></div>
                <div class='schedbar_m' style='width: 490px;'>
                    <span class='start'><img src='/Images/Schedule/arrowLeft.gif' alt='' style='margin-left:5px; margin-top:2px;' /></span>
                    <div class='OTtext' align='center'>All Day</div>
                    <span class='end'></span>
                </div>
                <div class='schedbar_r'></div>
            </div>
            <div class='availbar' style='left: 508px; width: 216px; display: none;'>
                <div class='schedbar_l_on'></div>
                <div class='schedbar_m_on' style='width: 208px;'><span class='start'></span>
                    <div class='OTtext' align='center'>All Day</div>
                    <span class='end'><img src='/Images/Schedule/arrowRight.gif' alt='' style='margin-left:5px; margin-top:2px;' /></span>
                </div>
                <div class='schedbar_r_on'></div>
            </div>
        </div>
    </td>
    <td>&nbsp;</td>
    <td class='rightColDetails'>
        <div class='AvailDetails' align='left' style='display: table-cell;'>
            <span class='iefix'><b>Avail - All Day</b></span><br/>
            <span style='font-size: 11px;'>Pref - All Day</span>
        </div>
    </td>
</tr>

这是一个分开的班次,两个四小时的轮班被一个空的1小时位置分开(他们这样做是为了欺骗得分系统,两个被覆盖的轮班而不是一个):

<tr>
    <td class='dt'>
        <span class='dow1'>Thursday</span>Dec 13
    </td>
    <td class='ScheduledDetails' valign='top'>
        <div style="position:relative;">
            <span class='tlHours'>8<span class='spart'> hrs</span> 0<span class='spart'> mins</span></span><span class='department'>Cashier</span><span class='meal'>Meal: None</span>
        </div>
    </td>
    <td>&nbsp;</td>
    <td class='Schedunderlay'>
        <div class='Sched'>
            <div class='schedbar' style='left: 247px; width: 104px;'>
                <div class='schedbar_l'></div>
                <div class='schedbar_m' style='width: 96px;'>
                    <span class='start'>2pm</span><span class='end'>6pm</span>
                </div><div class='schedbar_r'></div>
            </div>
            <div class='schedbar' style='left: 377px; width: 104px;'>
                <div class='schedbar_l'></div>
                <div class='schedbar_m' style='width: 96px;'>
                    <span class='start'>7pm</span> <span class='end'>11pm</span>
                </div>
                <div class='schedbar_r'></div>
            </div>
            <div class='availbar' style='left: 9px; width: 498px; display: none;'>
                <div class='schedbar_l'></div><div class='schedbar_m' style='width: 490px;'>
                    <span class='start'><img src='/Images/Schedule/arrowLeft.gif' alt='' style='margin-left:5px; margin-top:2px;' /></span>
                    <div class='OTtext' align='center'>All Day</div>
                    <span class='end'></span>
                </div>
                <div class='schedbar_r'></div>
            </div>
            <div class='availbar' style='left: 508px; width: 216px; display: none;'>
                <div class='schedbar_l_on'></div>
                <div class='schedbar_m_on' style='width: 208px;'>
                    <span class='start'></span>
                    <div class='OTtext' align='center'>All Day</div>
                    <span class='end'><img src='/Images/Schedule/arrowRight.gif' alt='' style='margin-left:5px; margin-top:2px;' /></span>
                </div>
            <div class='schedbar_r_on'></div>
        </div>
    </div>
    </td>
    <td>&nbsp;</td>
    <td class='rightColDetails'>
        <div class='AvailDetails' align='left' style='display: table-cell;'>
            <span class='iefix'><b>Avail - All Day</b></span><br/><span style='font-size: 11px;'>Pref - All Day</span>
        </div>
    </td>
</tr>

重要的区别在于常规班次有一个开始和一个结束时间,分开班次有一个开始,结束,开始和结束......

我现在一直在冲击这个大约五个小时...并且没有取得进展,我想如果我理解正则表达式我会有更多的运气......任何帮助都会非常感激...

1 个答案:

答案 0 :(得分:1)

以下是使用BeautifulSoup解析文档并获取信息的解决方案。

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

for schedbar in soup.find_all('div', 'schedbar'):
  print "start: " +  schedbar.find('div', 'schedbar_m').find('span', 'start').string
  print "end: " +  schedbar.find('div', 'schedbar_m').find('span', 'end').string

输出:

start: 2pm
end: 6pm
start: 7pm
end: 11pm