String.find没有返回任何值

时间:2011-08-26 10:33:58

标签: python string

我正在尝试对以下值进行子字符串以从中获取链接(图像值):

<p>This is a recipe developed by my wife, Irene. It&rsquo;s been tweaked to perfection. I've never ordered congee in Manila since then.</p>
<p style="text-align: center;">
(img src="http://img35.imageshack.us/img35/6047/69934653.jpg" alt="" >
</p>

我尝试了以下内容:

thumb = item.xpath('description')[0].text // To get the vaue from XML
            pos = thumb.find('http')   // Find http: and then Substring
            Log('Position HTTP'+pos)
            thumb = thumb[:pos]
            Log('Thumb Details'+thumb)

2 个答案:

答案 0 :(得分:2)

str.find()返回int,因此Log('Position HTTP'+pos)将引发TypeError

更改为Log('Position HTTP %s' % pos)

答案 1 :(得分:1)

也许您应该考虑使用Regex模块: http://docs.python.org/library/re.html

import re
match = re.search('http://\S+.(?:jpeg|jpg|png)', a_string)
print match.group(0)