2016-03-21 67 views
1
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代碼從網站獲取價值?

+3

可能的重複[如何在Python中修改文本文件?](http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python) – Nitish

回答

0

最簡單的方式不需要編碼。只需將腳本的輸出重定向到文件,例如

python yahoo_scraper.py > prices.txt 

python yahoo_scraper.py >> prices.txt 

追加到現有文件。

在Python中做它也很容易。打開文件進行寫入和寫入:

with open('prices.txt', 'w') as price_file: 
    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, file=price_file) 
     i=i+1 

請注意,每次腳本運行時都會覆蓋該文件。如果要附加到文件末尾,請以'w'替換爲'a'以附加模式打開它。

你的while循環會更好地寫成for循環。下面是一個例子 - 我假定list被重命名爲stocks以避免陰影內建list

stocks = ["infy.ns","grasim.ns",....] 

with open('prices.txt', 'w') as price_file: 
    for stock in stocks: 
     url = "http://finance.yahoo.com/q?s={}&q1=1".format(stock) 
     html = urllib.urlopen(url).read() 
     pattern = r'<span id="yfs_l84_{}>(.+?)</span>'.format(stock) 
     price = re.findall(pattern, html) 
     print(price, file=price_file) 

您可能需要更改的最後一行打印由re.findall()返回列表的第一個元素。

+0

我得到這個錯誤,當我編譯與什麼是解決方案打印(價格,文件=價格文件) ^ SyntaxError:無效的語法 –

相關問題