对于我的股票筛选工具,我必须在脚本中从 BeautifulSoup 切换到 lxml。在我的 Python 脚本下载了我需要处理的网页后,BeautifulSoup 能够正确解析它们,但处理速度太慢。分析一只股票的资产负债表、损益表和现金流量表需要 BeautifulSoup 大约 10 秒,考虑到我的脚本有超过 5000 只股票要分析,这个速度慢得让人无法接受。
根据一些基准测试(http://www.crummy.com/2012/1/22/0),lxml 比 BeautifulSoup 快近 100 倍。因此,lxml 应该能够在 10 分钟内完成一项需要 BeautifuSoup 14 小时的工作。
如何使用 HTML 来捕获 HTML 表格中一行的内容?我的脚本已下载并需要解析的 HTML 页面示例位于http://www.smartmoney.com/quote/FAST/?story=financials&opt=YB
使用 BeautifulSoup 解析这个 HTML 表格的源代码是:
url_local = local_balancesheet (symbol_input)
url_local = "file://" + url_local
page = urllib2.urlopen (url_local)
soup = BeautifulSoup (page)
soup_line_item = soup.findAll(text=title_input)[0].parent.parent.parent
list_output = soup_line_item.findAll('td') # List of elements
如果我正在寻找现金和短期投资,title_input = "现金和短期投资"。
如何在 lxml 中执行相同的功能?