【发布时间】:2016-08-16 12:16:23
【问题描述】:
我正在尝试使用 BeautifulSoup 和 RE 从 Yahoo Finance 获得特定价值。我无法弄清楚如何获得它。我将粘贴一些我拥有的代码以及我得到的 HTML 和唯一选择器。
我只想要这个数字,“7.58”,但问题是该列的类与同一元素中的许多其他类相同。
<tr><td class="yfnc_tablehead1" width="74%">Diluted EPS (ttm):</td><td class="yfnc_tabledata1">7.58</td>"
这是 Google 给我的选择器...
yfncsumtab > tbody > tr:nth-child(2) > td.yfnc_modtitlew1 > table:nth-child(10) > tbody > tr > td > table > tbody > tr:nth-child(8) > td. yfnc_tabledata1
这是我用来测试不同事物的一些模板代码,但我对正则表达式非常陌生,在“稀释 EPS (ttm):###
from bs4 import BeautifulSoup
import requests
import re
sess = requests.Session()
res = sess.get('http://finance.yahoo.com/q/ks?s=MMM+Key+Statistics')
soup = BeautifulSoup(res.text, 'html.parser')
body = soup.findAll('td')
print (body)
谢谢!
【问题讨论】:
-
你为什么使用BS 和正则表达式?事实上,我没有看到任何尝试在代码中使用任何一种来做你想做的事情。
-
我不知道 BS 命令让它找到文本短语后面的数字。
标签: python regex web-scraping beautifulsoup