【发布时间】:2014-04-25 16:51:01
【问题描述】:
我正在尝试学习 python 和 BS4,我正在尝试从使用 BS4 的页面中提取一些 frames 和 iframes,如下所示:
#...snip...
soup_f = soup("frame")
if soup_f is not None:
for frame in soup_f:
try:
t_iFrames_src.append(force_text(soup.frame.extract().get("src"), encoding='utf-8', strings_only=False, errors='strict'))
except (AttributeError, UnicodeEncodeError):
pass
try:
t_full_frame.append(force_text(soup.frame.extract(), encoding='utf-8', strings_only=False, errors='strict'))
except (AttributeError, UnicodeEncodeError):
pass
else:
pass
问题是,当第一个 try..except 运行时,它会得到有效的结果(通过填充 t_iFrames_src),但由于某些奇怪的原因,第二个 try...except 没有给我任何结果。即t_full_frame 为空
所以,当我像这样翻转它们时:
#...snip...
soup_f = soup("frame")
if soup_f is not None:
for frame in soup_f:
try:
t_full_frame.append(force_text(soup.frame.extract(), encoding='utf-8', strings_only=False, errors='strict'))
except (AttributeError, UnicodeEncodeError):
pass
try:
t_iFrames_src.append(force_text(soup.frame.extract().get("src"), encoding='utf-8', strings_only=False, errors='strict'))
except (AttributeError, UnicodeEncodeError):
pass
else:
pass
现在,t_full_frame 有结果,但 t_iFrames_src 是空的。我很困惑为什么会这样 :(
可能这是非常愚蠢的事情,但我无法弄清楚出了什么问题! 如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】: