【发布时间】:2014-07-03 05:39:54
【问题描述】:
我正在尝试在字符串中查找属性的值。在<img src="invalidURL.com"> 中,如果属性/子字符串是src,我想收到invalidURL.com。
在 Violent Python 中,它使用imgSrc = imgTag['src'] 行,这不会产生编译器错误,并且脚本运行良好。 (完整的脚本可以在this Github repo. 找到)但是,当我尝试编写自己的脚本时,它给出了编译器错误。
htmlImgTags = ['<img src="/images/icons/product/chrome-48.png"/>', '<img src="asdasd">']
for tag in htmlImgTags:
print tag
tagSrc = tag['src']
print tagSrc
该错误抱怨使用字符串作为索引而不是 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
我的代码到底出了什么问题,但书中没有?
【问题讨论】: