import urllib
from datetime import date,timedelta
import datetime
import re
list =["infy.ns","grasim.ns","idea.ns","asianpain.ns","bajaj-auto-eq.ns",
"drreddy.ns","boschltd.ns","kotakbank.ns","M&M.ns","ultracemc.ns",
"sunpharma.ns","lt.ns","acc.ns","sbin.ns","bhartiartl.ns",
"lupin.ns","reliance.ns","hdfcbank.ns","zeel.ns","ntpc.ns",
"icicibank.ns","cipla.ns","tcs.ns","bpcl.ns","heromotoc.ns"]
i=0
while i<len(list):
url="http://finance.yahoo.com/q?s="+list[i]+"&ql=1"
htmlfile = urllib.urlopen(url)
htmltext=htmlfile.read()
regex='<span id="yfs_l84_'+list[i]+'">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern,htmltext)
print(price)
i=i+1
我不得不從finance.yahoo.com 取值當我通過使用終端運行的代碼,然後我得到了終端的所有價值,但我希望把該值在我的桌面上的文本文件我們如何通過使用python代碼從網站獲取價值?
可能的重複[如何在Python中修改文本文件?](http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python) – Nitish