How to search for a substrings value inside of a string?

Wednesday, July 2, 2014

I am trying to find the value of a property, inside of a string. In <img src="invalidURL.com"> if the property/sub-string were to be src, I would want to recieve invalidURL.com.


In Violent Python it uses the line imgSrc = imgTag['src'], which does not givee a compiler error, and the script runs fine. (Full script can be found in this Github repo.) However, when I tried to write my own script it gives a compiler error.



htmlImgTags = ['<img src="/images/icons/product/chrome-48.png"/>', '<img src="asdasd">']
for tag in htmlImgTags:
print tag
tagSrc = tag['src']
print tagSrc


The error complains about using a string as an index instead of an int.



<img src="/images/icons/product/chrome-48.png"/>
Traceback (most recent call last):
File "looking in an array.py", line 4, in <module>
tagSrc = tag['src']
TypeError: string indices must be integers, not str


What exactly is going wrong in my code, but not in the book's?







http://ift.tt/1xkhkQ7