如何将python的“打印”写入txt文件?

2024-05-31 12:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium和Pycharm抓取一个网页,到目前为止,这是我的代码:

from selenium import webdriver
import time
mozilla_path = r"C:\Users\ivrav\Python38\geckodriver.exe"
driver = webdriver.Firefox()
driver.get("https://www.bundesbank.de/en/bundesbank/research/research-centre/people/people-738992")
time.sleep(3)
driver.maximize_window()
driver.find_element_by_xpath("""//*[@id="top"]/div/div[3]/div/div/div/div/main/div/div[1]/div/div[1]/nav/ul/li/div/a""").click()
Researcher=driver.find_element_by_class_name("main")
print(Researcher.text)

印刷品如下:

Emanuel Mönch
Head of Research
27.06.2019
Research Interests
Risk Premium Modeling
Financial Intermediation, Asset Prices, and Macroeconomic Dynamics
Asset Price Anomalies
Business Cycle Analysis and Macroeconomic Forecasting
Modeling Macroeconomic Expectations
Refereed Publications
Ghysels, E., C. Horan and E. Moench (2018), Forecasting through the Rear-view Mirror: Data Revisions and Bond Return Predictability, Review of Financial Studies, Vol. 31(2), pp. 678-714.
Abrahams, M., T. Adrian, R. K. Crump, E. Moench and R. Yu (2016), Decomposing Real and Nominal Yield Curves, Journal of Monetary Economics, Vol. 84, pp. 182-200.
Andrade, P., R. K. Crump, S. Eusepi and E. Moench (2016), Fundamental Disagreement, Journal of Monetary Economics, Vol. 83, pp. 106-128.
Liu, W. and E. Moench (2016), What Predicts U.S. Recessions, International Journal of Forecasting, Vol. 32(4), pp. 1138–1150.
Adrian, T., R. K. Crump and E. Moench (2015), Regression-Based Estimation of Dynamic Asset Pricing Models, Journal of Financial Economics, Vol. 118 (2), pp. 211–244.
Lucca, D. and E. Moench (2015), The Pre-FOMC Announcement Drift, Journal of Finance, Vol. 70(1), pp. 329-371, winner of the Amundi Smith Breeden First Prize for the best capital markets paper published in the Journal of Finance in 2015.
Adrian, T., R. K. Crump and E. Moench (2013), Pricing the Term Structure with Linear Regressions, Journal of Financial Economics, Vol. 110(1), pp. 110-138.
Moench, E., S. Ng, and S. Potter (2013), Dynamic Hierarchical Factor Models, Review of Economics and Statistics, Vol. 95(5), pp. 1811-1817.
Moench, E. (2012), Term Structure Surprises: The Predictive Content of Curvature, Level, and Slope, Journal of Applied Econometrics, Vol. 27(4), pp. 574-602.
Carvalho, C., N. Klagge, and E. Moench (2011), The Persistent Effects of a False News Shock, Journal of Empirical Finance, Vol 18(4), pp. 597-615.
Moench, E. and S. Ng (2011), A Hierarchical Factor Analysis of US Housing Market Dynamics, The Econometrics Journal, Vol. 14(1), pp. C1-C24.
Adrian, T., E. Moench, H.S. Shin (2010), Macro Risk Premium, and Intermediary Balance Sheet Quantities, IMF Economic Review, Vol 58(1), pp. 179-207.
Aragon, D., E. Moench, and J. Vickery (2010), Why is the Market Share of Adjustable-Rate Mortgages so Low? Current Issues in Economics and Finance, Vol. 16(8).
Mackowiak, B., E. Moench and M. Wiederholt (2009), Sectoral Price Data and Models of Price Setting, Journal of Monetary Economics, Vol. 56, pp. S78-S99.
Moench, E. (2008), Forecasting the Yield Curve in a Data-Rich Environment: A No-Arbitrage Factor-Augmented VAR Approach, Journal of Econometrics, Vol. 146(1), pp. 26-43.
Moench, E. and H. Uhlig (2005), Towards a Monthly Business Cycle Chronology for the Euro Area, Journal of Business Cycle Measurement and Analysis, Vol. 2(1), pp. 43-69.
Working Papers
Crump, Richard K., S. Eusepi, and E. Moench (2018), The Term Structure of Expectations and Bond Yields, Federal Reserve Bank of New York Staff Reports No. 775, April 2018.
www.newyorkfed.org
Carvalho, Carlos, S. Eusepi, E. Moench, and B. Preston (2017): Anchored Inflation Expectations. (available at SSRN).
papers.ssrn.com
Adrian, Tobias, E. Moench and H. S. Shin (2016), Dynamic Leverage Asset Pricing, CEPR Discussion Paper No. DP11466, August 2016.
cepr.org
Adrian, Tobias, E. Moench and H.S. Shin (2010), Financial Intermediation, Asset Prices, and Macroeconomic Dynamics, Federal Reserve Bank of New York Staff Reports No. 422, September 2010.
www.newyorkfed.org
Awards
Amundi Smith Breeden First Prize, Journal of Finance, 2015
Young Economist Award, European Economic Association, 2008
Research Brief
The impact of Eurosystem bond purchases on the repo market
Research Brief | 21st edition – September 2018
28.09.2018 | Stephan Jank, Emanuel Mönch
Other Publications
VoxEU, Liberty Street Economics Blog
Lucca, D. and E. Moench (November 2018), "The Pre-FOMC Announcement Drift: More Recent Evidence"
libertystreeteconomics.newyorkfed.org
Andrade, P., R. K. Crump, S. Eusepi and E. Moench (December 2014), "Learning from disagreement: Evidence from forecasters"
www.voxeu.org
Crump, R. K., S. Eusepi, D. Lucca, and E. Moench (December 2014), "Data Insight: Which Growth Rate? It's a Weighty Subject"
libertystreeteconomics.newyorkfed.org
O'Boyle, W., R. K. Crump, E. Moench, M. Raskin, C. Rosa and L. Stowe (December 2014), "Interest Rate Derivatives and Monetary Policy Expectations"
libertystreeteconomics.newyorkfed.org
O'Boyle, W., R. K. Crump, E. Moench, M. Raskin, C. Rosa and L. Stowe (December 2014), "Survey Measures of Expectations for the Policy Rate"
libertystreeteconomics.newyorkfed.org
Crump, R. K., T. Davig, S. Eusepi and E. Moench (September 2014), "Connecting the Dots: Disagreement in the Federal Open Market Committee"
libertystreeteconomics.newyorkfed.org
Adrian, T., R. K. Crump, B. Mills and E. Moench (May 2014), "Treasury Term Premia: 1961-Present"
libertystreeteconomics.newyorkfed.org
Crump, R. K., S. Eusepi, and E. Moench (September 2013), "Preparing for Takeoff? Professional Fore-casters and the June 2013 FOMC Meeting"
libertystreeteconomics.newyorkfed.org
Adrian, T., R. K. Crump, and E. Moench (April 2013), "Do Treasury Term Premia Rise around Monetary Tightenings?"
libertystreeteconomics.newyorkfed.org
Crump, R., S. Eusepi, and E. Moench (January 2013), "Making a Statement: How Did Professional Fore-casters React to the August 2011 FOMC Statement?"
libertystreeteconomics.newyorkfed.org
Lucca, D. and E. Moench (July 2012), "The Puzzling Pre-FOMC Announcement 'Drift' "
libertystreeteconomics.newyorkfed.org
Carvalho, C., N. Klagge, and E. Moench (October 2011), "How Well Do Financial Markets Separate News from Noise? Evidence from an Internet Blooper"
libertystreeteconomics.newyorkfed.org
Crump, R. K., S. Eusepi and E. Moench (August 2011), "A Look at the Accuracy of Policy Expectations"
libertystreeteconomics.newyorkfed.org

要在txt文件中写入打印,代码如下:

task=open("C:\Deutsche Bundesbank\Web_page_Bundesbank.txt", "a")
task.write(str(Researcher) + "\n")
task.close()

但是,txt文件中的结果如下:

<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="52f7ac63-cd49-4643-b73a-952960dc4996", element="8a15ed96-ec84-49c3-a9c8-4774db8ca86e")>

在txt文件中写入打印的正确方法是什么?任何建议都会有帮助

非常感谢,, 伊万


Tags: andoftheorgdivppjournaladrian
2条回答

您的代码是将字符串转换为字符串,而不是像在打印中那样获取文本。由于该项不会覆盖默认的__str__方法,Python只会告诉您它可以对研究者做些什么,这就是您看到的WebElement实例

与其这样做,不如修改代码以编写与打印内容完全相同的内容

with open("C:\Deutsche Bundesbank\Web_page_Bundesbank.txt", "a") as f:
    f.write(Researcher.text + "\n")

我还将您的文件打开方式更改为更具python风格的方式,with操作符将为您处理关闭文件的操作

在以下情况下,您正在将研究者对象的字符串表示形式写入文件

task.write(str(Researcher) + "\n")

要将打印内容写入文件,您可以稍微调整代码:

task=open("C:\Deutsche Bundesbank\Web_page_Bundesbank.txt", "a")
task.write(Researcher.text + "\n")
task.close()

但是,我建议使用with语句写入文件,因为当到达with块末尾时,它会自动处理文件关闭:

with open("C:\Deutsche Bundesbank\Web_page_Bundesbank.txt", "a") as task:
    task.write(Researcher.text + "\n")

相关问题 更多 >