【发布时间】:2015-02-01 11:42:10
【问题描述】:
我很难通过 Python 3、BeautifulSoup 4 抓取此链接
http://www.radisson.com/lansing-hotel-mi-48933/lansing/hotel/dining
我只想得到这个部分。
When you are in ...
Capitol City Grille
This downtown Lansing restaurant offers ...
Capitol City Grille Lounge
For a glass of wine or a ...
Room Service
If you prefer ...
我有这个代码
for rest in dining_page_soup.select("div.copy_left p strong"):
if rest.next_sibling is not None:
if rest.next_sibling.next_sibling is not None:
title = rest.text
desc = rest.next_sibling.next_sibling
print ("Title: "+title)
print (desc)
但它给了我TypeError: 'NoneType' object is not callable
在desc = rest.next_sibling.next_sibling 上,即使我有一个if 语句来检查它是否是None。
【问题讨论】:
-
尝试使用
if not rest.next_sibling is None和if not rest.next_sibling.next_sibling is None代替上面的两个if 语句,看看你是否得到了一些有用的提示? -
你能把它发布为答案
-
抱歉,这里的社区会因为发布我不确定的任何答案而在此处投反对票,所以.. 只需将
for rest in dining_page_soup.select("div.copy_left p strong"):之后的两个 if 条件替换为上述评论中的 if 条件订购 -
还是一样的错误
-
尝试将
title = rest.text行移到desc = rest.next_sibling.next_sibling行下方?
标签: python python-3.x beautifulsoup