【发布时间】:2014-12-03 18:57:01
【问题描述】:
我正在抓取这个URL。
我必须抓取页面的主要内容,例如 Room Features 和 Internet Access
这是我的代码:
for h3s in Column: # Suppose this is div.RightColumn
for index,test in enumerate(h3s.select("h3")):
print("Feature title: "+str(test.text))
for v in h3s.select("ul")[index]:
print(v.string.strip())
此代码会抓取所有 <li> 的内容,但在抓取 Internet Access 时
我明白了
AttributeError: 'NoneType' object has no attribute 'strip'
因为 Internet Access 标题下的 <li>s 数据包含在双引号内,例如 "Wired High Speed Internet Access..."
我尝试将print(v.string.strip()) 替换为print(v),结果为<li>Wired High...</li>
我也尝试过使用print(v.text),但它也不起作用
相关部分如下所示:
<h3>Internet Access</h3>
<ul>
<li>Wired High Speed Internet Access in All Guest Rooms
<span class="fee">
25 USD per day
</span>
</li>
</ul>
【问题讨论】:
-
没有双引号;也许您对浏览器开发者工具中的元素树视图感到困惑?
-
如果你在谷歌浏览器中检查......你会看到双引号,没有跨度......但在 Firefox 中,没有 2ble-quotes,它也显示跨度
-
Chrome 中也有跨度,需要向下滚动。引号作为开发人员工具的一部分显示字符串值周围有多少空格(制表符和换行符)。它们不是该元素实际值的一部分。
-
he quotes are there as part of the developer tool to show how much whitespace谢谢亲爱的...新信息
标签: python html python-3.x beautifulsoup