Python boolean expression always true (when it shouldn't be)
I have a method that is intended to grab all img elements from some html and to add a css style to ensure the image is resized if it is large. It works great until the final test : largest_size < img_size - I have tried all manner of different ways to express this simple thing, and yet it always evaluates to true - which means all images are resized regardless of their original size.
The code:
def adjust_html(self, html_text):
# pull image links and adjust those larger than 30k
# to be width=100%
html = 开发者_如何学运维etree.HTML(html_text)
r = html.xpath('.//img')
changed_text = False
for elem in r:
for tag, value in elem.attrib.iteritems():
if tag == 'src':
largest_size = 30720
img_size = 0
img_url = value
if self.bad_urls.has_key(img_url):
break
try:
usock = urllib2.urlopen(img_url)
img_size = usock.info().get('Content-Length')
except:
self.log.debug("***** 406 for " + img_url)
self.bad_urls[img_url] = True
break
if img_size is None:
break
else:
**if (largest_size < img_size):**
self.log.debug("*** " + img_url + " ***")
self.log.debug("********** img size = " + str(img_size) + " **********")
elem.set("style","width:100%")
changed_text = True
break
if changed_text == True:
html_text = etree.tostring(html)
return html_text
I know there has to be something simple wrong here - I just don't see it :)
int
is always less than str
. Turn your header value into an int
first. And remember, debug with repr()
, not str()
.
img_size is a string not an int:
>>> 30720 < '0'
True
精彩评论