删除& nbsp beautifulsoup

技术标签: HTML.  Python-2.7.  解析  美丽群组

我正在尝试在此刻解析时间表网页,我用打印出来的问题就是有关于死亡空间的精神。这是由于我在很多情况下看到的标签是“& nbsp”作为文本条目。当我们没有课程时,那个空间就在那里。

from bs4 import BeautifulSoup
import urllib2
url="http://timetable.ait.ie/reporting/individual;student+set;id;AL%5FKNTWM%5F7%5F3%0D%0A?t=student+set+individual&days=1-5&weeks=&periods=3-20&template=student+set+individual"
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read(), 'lxml')
sources = soup.findAll('table')
for source in sources:
    for tr in source.findAll('tr'):
        for td in tr.findAll('td',{"rowspan":True}):
            if td.text is u'\xa0': continue             
            else:
                print td.text

这是我到目前为止的代码,我已经尝试过 if td.text is '&nbsp': continueif td.text == '&nbsp': continueif td.text == u'\xa0': continue 但这些选项都没有为我工作。这是我正在尝试解析的HTML代码的一部分。

<!-- START ROW OUTPUT -->
                <tr >
                    <td style="border-bottom:3px solid #000000;" rowspan='1' >Mon</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;"  colspan='2' rowspan='1' >
                        <!-- START OBJECT-CELL -->
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <tr>
                                <td align='left'><font color='#000000'>Networks Security 3</font></td>
                            </tr>
                        </table>
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <col align='right' />
                            <tr>
                                <td align='left'><font color='#000000'>W206</font></td>
                                <td align='right'><font color='#000080'>Bennett, Tom</font></td>
                            </tr>
                        </table>
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <tr>
                                <td align='left'><font color='#000080'>3-9, 11-14</font></td>
                            </tr>
                        </table>
                        <!-- END OBJECT-CELL -->

如您所见,有多个“TD”标签具有&amp; nbsp作为一个选项,但稍后我需要计算这些不破坏的空格来计算类的时间。任何帮助将不胜感激。

编辑:请忽略循环中的Rowspan我复制了代码,并犯了一个小错误。

看答案

html = '''
        <!-- START ROW OUTPUT -->
                <tr >
                    <td style="border-bottom:3px solid #000000;" rowspan='1' >Mon</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;" >&nbsp;</td>
                    <td style="border-bottom:3px solid #000000;"  colspan='2' rowspan='1' >
                        <!-- START OBJECT-CELL -->
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <tr>
                                <td align='left'><font color='#000000'>Networks Security 3</font></td>
                            </tr>
                        </table>
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <col align='right' />
                            <tr>
                                <td align='left'><font color='#000000'>W206</font></td>
                                <td align='right'><font color='#000080'>Bennett, Tom</font></td>
                            </tr>
                        </table>
                        <table  cellspacing='0' border='0' width='100%'>
                            <col align='left' />
                            <tr>
                                <td align='left'><font color='#000080'>3-9, 11-14</font></td>
                            </tr>
                        </table>
                        <!-- END OBJECT-CELL -->'''

from bs4 import BeautifulSoup
import re

soup = BeautifulSoup(html, 'lxml')

tds = soup.find_all(name='td', text=re.compile(r"^\S"))
for td in tds:
    print(td)

出去:

<td rowspan="1" style="border-bottom:3px solid #000000;">Mon</td>
<td align="left"><font color="#000000">Networks Security 3</font></td>
<td align="left"><font color="#000000">W206</font></td>
<td align="right"><font color="#000080">Bennett, Tom</font></td>
<td align="left"><font color="#000080">3-9, 11-14</font></td>

智能推荐

BeautifulSoup库

BeautifulSoup类的基本元素: 标签树的下行遍历: tag.contents返回的是一个列表类型,可以通过len()返回它的长度,可以得知tag.contents的儿子节点不仅包括标签节点还包括字符串节点。 标签树的上行遍历 由于html是整个文本最高级的标签,所以它的父亲标签就是整个文本。而soup可以说是真正的顶层,所以soup.parent是空。 标签树的平行遍历...

运行BeautifulSoup

提取https://www.pythonscraping.com/pages/page1.html页面的<h1>标签 注:html.parser是python3中的一个解析器 运行结果: BeautifulSoup还可以使用urlopen直接返回文本对象,而不需要先调用.read函数: 运行结果: 可以看出,我们从网页提取的<h1>标签镶嵌在BeautifulSoup对象结...

踩中&nbsp;的坑

1、在html代码中&nbsp;和空格的区别: 在html代码中每输入一个转义字符&nbsp就表示一个空格,输入十个&nbsp,页面中就显示10个空格位置。 而在html代码中输入空格,不管输入多少个空格,最终在页面中显示的空格位置只有一个 2、英文句子中用&nbsp;替代空格,会导致句子不能正常断行 它叫不换行空格,全称No-Break Space 3、&...

【原】Linux&nbsp;终端分屏工具&nbsp;tmux&nbsp;配…

本文是让linux下分屏工具可以支持鼠标选窗口或面板,鼠标调大小,鼠标滚屏而做的设置。 先贴出主目录下的.tmux.conf文件如下 ----------  .tmux.conf  START ------------------------------- set -g prefix ^a unbind ^b...

HTML学习---------1.11空格标签&nbsp;

在HTML中输入空格是没有用的,要想输入空格,就必须用   &nbsp;  标签; 在HTML代码只能怪输入空格是不起作用的,如下代码: 在浏览器中显示,是没有空格效果的. 输入空格的正确方式: 在浏览器中显示出来的空格效果如下:...

猜你喜欢

「机器学习_5」逻辑回归(Logistic Regression )

学习逻辑回归需要清楚的几件事:假设函数怎么用(怎么预测),决策边界怎么用(一般取0.5),损失函数如果计算(因为要取最小),优化方法(重新计算各个权重) 建议:我阅读的英文参考资料的相关内容除了公式我并没有往上发布。但是我会放到我的下载资料中,建议,想彻底理解这几个内容的,如果不想直接从公式推导入手,可以考虑看下具体的例子,就会明白其中的含义。   前言  本文主要介绍逻辑回归的基础知识...

工业4.0知识图谱

编者按         本篇文章来自于17th Extended Semantic Web Conference (ESWC2020) 会议。该文章提出了基于语义标注网络的结构化数据集,包含工业4.0标准、范式和框架,并给出了知识图谱在工业4.0应用的现有案例。   本文作者北京大学周雨奇,将对原文进行概要阐述。   文末提供获...

缓冲区溢出实例(二)–Linux

原理:crossfire 1.9.0 版本接受入站 socket 连接时存在缓冲区溢出漏洞。 工具: 调试工具:edb; ###python在漏洞溢出方面的渗透测试和漏洞攻击中,具有很大的优势 实验对象:crossfire【多人在线RPG游戏】 运行平台:Kali i386 虚拟机【32位,计算机CPU位数是指地址总线位数,64位系统的寻址空间为2^64,寻址过大,难以处理,为了简化操作,所以选用...

如何使用物联网设备来确保儿童安全? | Linux 中国

物联网设备提供的好处很多。举例来说,父母可以远程监控自己的孩子,而又不会显得太霸道。因此,儿童在拥有安全环境的同时也会有空间和自由让自己变得独立。 -- Andrew Carroll IoT (物联网)设备正在迅速改变我们的生活。这些设备无处不在,从我们的家庭到其它行业。根据一些预测数据,到 2020 年,将会有 100 亿个 IoT 设备。到 2025 年,该数量将增长到 220 亿。目前,物联...

windows 组策略

通过 Windows 组策略,实现登录启动指定脚本。 1、快捷键 win+R,输入 gpedit.msc,点击 ok 2、在 Local Group Policy Editor 对话框中选择:Local Computer Policy --> User Configuration --> Windows Settings – > Scripts,然后点击右侧 Logo...

问答精选

insertMany drop down mongodb service

I have API, where I get datas. I use mongoose to save it into my local MongoDB. Each time when I save, I create dynamically new model and use insertMany on it: But... later, when all almost complete, ...

How to use Task.WhenAll to wait for third party Async method call

I have been trying to figure this out for a few hours and haven't been able to understand why this isn't working. I have a TPL Dataflow batch block that accepts a batch of files that I want to upload....

Python Pandas KeyError when trying to save a timestamp to MySQL

I'm trying to save a data frame to a mysql database. But it seems that it doesn't work because of the timestamp (format?). When I run this script, the following Error occurs ..... Can't figure it out ...

Auto renumbering sections with Notepad++ automated scripts

I am a modder with very little scripting experience. I need a way to renumber the following Sections of a file: etc. The file looks like this: and has to be continued like this: and so on. So simply s...

Write log-file when testing with NUnit

I have a test-assembly (MyTestProject) where I want to write some logging using log4net. Thus I created a config-file with the same name as the assembly where I set up the logging as suggested here: W...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答