【发布时间】:2022-01-22 17:47:38
【问题描述】:
我正在尝试从 Morning Star 网站提取公司列表的两个数据点并将其保存到文本文件中,但我不确定如何处理此任务。以下是我的代码:
from bs4 import BeautifulSoup as BS
thislist = ["AAPL","FB","TSLA","DIS"]
for symbol in thislist:
print ('Getting data for ' + symbol + '...\n')
# extract from this website
url="https://www.morningstar.com/stocks/xnas/" + symbol + "/quote"
soup = BS(url)
# Find the Value of Last Close Price
for text in soup.find_all('div class', name_='Last Close'):
Last_Close = text.find_all('dp-value price-down')
print(Last_Close)
# Find the Value of its Market Cap
for text in soup.find_all('div class', name_='Market Cap'):
Market_Cap = text.find_all('dp-value')
print(Market_Cap)
# Print the table
print(symbol, Last_Close, Market_Cap)
# Save the data in a .txt file
df.to_csv(r'c:\data\testing.txt', header=None, index=None, sep=' ', mode='a')
【问题讨论】:
-
好的,但有什么问题?你必须清楚地说明为什么这不是你想要的How do I ask a good question?。请向我们展示现有的输出或错误消息。
-
另外,请务必标记 Python 问题python,以便人们更快地看到它们。而这个也是关于web-scraping。 (你可以浏览现有的问答,有很多关于这个的现有问题。)但你仍然需要陈述一个具体的问题。请浏览SO Help 以了解如何表达问题。
标签: python web-scraping beautifulsoup