我正在尝试使用以下代码:
for x in story:
var1 = str(x)
var1 = var1.replace("<p>", "\n")
var1 = var1.replace("</p>", "")
story[x] = var1
删除段落标记,并插入换行符,然后将它们重新插入变量中。字符串如下:
Panera Bread (NASDAQ: <a class="ticker" href="/stock/pnra#NASDAQ">PNRA</a>) is down 6 percent today over expectations of food inflation of 4.5% in Q3 and 5% for Q4. In addition, Panera Will Raise Menu Prices in Q4.
PNRA recently posted second quarter 2011 earnings of $1.18 per share. Reported earnings also outpaced the prior-year quarter earnings of 85 cents per share.
But shares were also lower ahead of the opening bell after the company reported weaker-than-expected same-store sales figures for its recent quarter late Tuesday. Its profit of $1.18 a share topped analysts' consensus call by a penny.
For the twenty-six weeks ended June 28, 2011, net income was $68 million, or $2.27 per diluted share. These results compare to net income of $53 million, or $1.67 per diluted share, for the twenty-six weeks ended June 29, 2010, and represent a 36% year-over-year increase in diluted earnings per share.
我收到的错误消息是:
Traceback (most recent call last):
File "C:\Python27\Sample Programs\Get Stuff from Pages\Pages and Stuff 0.1.py", line 34, in <module>
story[x] = var1
TypeError: list indices must be integers, not Tag
您可能希望将这些项附加到列表中:
for
循环迭代地将liststory
的元素替换成x
变量,而[]
list指令需要元素索引。这会导致错误。你知道吗编辑:我错过了故事不是由字符串组成的。这些更改可以完成以下任务:
注意:现在
story
由字符串组成,而不是标记。你知道吗相关问题 更多 >
编程相关推荐