【发布时间】:2026-01-30 08:50:01
【问题描述】:
我正在尝试阅读 html 网站并提取其数据。例如,我想阅读过去 5 年公司的 EPS(每股收益)。基本上,我可以阅读它并且可以使用 BeautifulSoup 或 html2text 来创建一个巨大的文本块。然后我想搜索该文件——我一直在使用 re.search——但似乎无法让它正常工作。这是我要访问的行:
EPS(基本)\n13.4620.6226.6930.1732.81\n\n
所以我想创建一个名为 EPS = [13.46, 20.62, 26.69, 30.17, 32.81] 的列表。
感谢您的帮助。
from stripogram import html2text
from urllib import urlopen
import re
from BeautifulSoup import BeautifulSoup
ticker_symbol = 'goog'
url = 'http://www.marketwatch.com/investing/stock/'
full_url = url + ticker_symbol + '/financials' #build url
text_soup = BeautifulSoup(urlopen(full_url).read()) #read in
text_parts = text_soup.findAll(text=True)
text = ''.join(text_parts)
eps = re.search("EPS\s+(\d+)", text)
if eps is not None:
print eps.group(1)
【问题讨论】:
-
我soup.prettify()后的html是: EPS (Basic)
13.46 20.62 26.69 30.17 32.81
标签: python html regex beautifulsoup html-parsing