我檢索使用下面的代碼從一個網站的經濟數據:如何格式化美麗湯和硒的輸出?
from bs4 import BeautifulSoup
from selenium import webdriver
url = 'https://www.fxstreet.com/economic-calendar'
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html, 'lxml')
for tr in soup.findAll('tr',{'class':['fxst-tr-event', 'fxst-oddRow', 'fxit-eventrow', 'fxst-evenRow', 'fxs_cal_nextEvent']}):
event = tr.find('div', {'class': 'fxit-event-title'}).text
currency = tr.find('div', {'class': 'fxit-event-name'}).text
actual = tr.find('div', {'class': 'fxit-actual'}).text
forecast = tr.find('div', {'class': 'fxit-consensus'}).text
previous = tr.find('div', {'class': 'fxst-td-previous fxit-previous'}).text
time = tr.find('div', {'class': 'fxit-eventInfo-time fxs_event_time'}).text
volatility = tr.find('div', {'class': 'fxit-eventInfo-vol-c fxit-event-info-desktop'}).span['title']
print(u'\t{}\t{}\t{}\t{}').format(time, currency, event, volatility)
從打印語句的輸出如下:
23:30
AUD
AiG Performance of Construction Index (Jul)
Moderate volatility expected
23:50
JPY
JP Foreign Reserves (Jul)
Low volatility expected
24h
CAD
August Civic Holiday
No volatility expected
01:30
AUD
ANZ Job Advertisements (Jun)
Low volatility expected
n/a
CNY
Foreign Exchange Reserves (MoM) (Jul)
Low volatility expected
05:00
JPY
Coincident Index (Jun)Preliminar
Moderate volatility expected
05:00
是否有可能格式化這個輸出,使得打印在線,如下所示?
23:30 AUD AiG Performance of Construction Index (Jul) Moderate volatility expected
23:50 JPY JP Foreign Reserves (Jul) Low volatility expected
24h CAD August Civic Holiday No volatility expected
01:30 AUD ANZ Job Advertisements (Jun) Low volatility expected
n/a CNY Foreign Exchange Reserves (MoM) (Jul) Low volatility expected
05:00 JPY Coincident Index (Jun)Preliminary Moderate volatility expected
最終目標是剪切此輸出並將其粘貼到Excel文件中。提前致謝!
也許嘗試去除換行? –
所以你想要這個..? – Beomi
print('somethins',end ='')#默認結束是\ n – Beomi