【发布时间】:2014-02-06 01:05:20
【问题描述】:
我有以下 HTML 代码:
<td class="image">
<a href="/target/tt0111161/" title="Target Text 1">
<img alt="target img" height="74" src="img src url" title="image title" width="54"/>
</a>
</td>
<td class="title">
<span class="wlb_wrapper" data-caller-name="search" data-size="small" data-tconst="tt0111161">
</span>
<a href="/target/tt0111161/">
Other Text
</a>
<span class="year_type">
(2013)
</span>
我正在尝试使用漂亮的汤将某些元素解析为制表符分隔的文件。 我得到了一些很大的帮助并且有:
for td in soup.select('td.title'):
span = td.select('span.wlb_wrapper')
if span:
print span[0].get('data-tconst') # To get `tt0082971`
现在我想获取“目标文本 1”。
我已经尝试了一些类似上述文本的内容,例如:
for td in soup.select('td.image'): #trying to select the <td class="image"> tag
img = td.select('a.title') #from inside td I now try to look inside the a tag that also has the word title
if img:
print img[2].get('title') #if it finds anything, then I want to return the text in class 'title'
【问题讨论】:
-
您是否尝试过自己提取它?
-
我已经编辑了上面的帖子
标签: python html parsing beautifulsoup