抓取的是中行的數(shù)據(jù):網(wǎng)址
代碼
# -*- coding:utf-8 -*- import re import urllib.request url = 'http://www.boc.cn/sourcedb/whpj/index.html' # 網(wǎng)址 req = urllib.request.Request(url) response = urllib.request.urlopen(req) the_page = response.read().decode('utf8') a = the_page.index('<td>澳大利亞元</td>') # 取得“澳大利亞元”當(dāng)前位置 s = the_page[a:a + 300] # 截取澳大利亞元匯率那部分內(nèi)容(從a到a+300位置) result = re.findall('<td>(.*?)</td>', s) # 正則獲取 with open('E:\匯率.txt', 'w+') as f: f.write(result[0] + '\n') f.write('現(xiàn)匯買入價:' + result[1] + '\n') f.write('現(xiàn)鈔買入價:' + result[2] + '\n') f.write('現(xiàn)匯賣出價:' + result[3] + '\n') f.write('現(xiàn)鈔賣出價:' + result[4] + '\n') f.write('中行折算價:' + result[5] + '\n') f.write('發(fā)布時間:' + result[6] + result[7] + '\n') if float(result[5]) >600: print('抓緊出手吧~') elif float(result[5]) < 500: print('要關(guān)注一下是不是要割肉了~') else: print('沒啥可做的,看看中行價格吧~' + '中行價格是:' + result[5] + '\n' + '詳細的匯率看E:\匯率.txt')