【问题标题】:Using Regular Expressions With Python to Get Value Buried in HTML5在 Python 中使用正则表达式来获取隐藏在 HTML5 中的值
【发布时间】: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


【解决方案1】:

你可以先通过短信Diluted EPS (ttm):找到:

soup.find('td', text='Diluted EPS (ttm):').parent.find('td', attrs={'class': 'yfnc_tabledata1'})

【讨论】:

  • 谢谢,我试试看能不能从那里找到号码。
  • 将此行放入编辑器时出现语法错误。你知道为什么吗? eps = soup.find('td', text='稀释 EPS (ttm):).parent.find('td', attrs={'class': 'yfnc_tabledata1'})
  • 对不起。在(ttm): 之后缺少单引号
  • 谢谢,正是我正在寻找的输出!非常感谢您的帮助。不管你信不信,我已经为此花费了至少 6 个小时并因此而偏头痛。也许我应该退出编程?
【解决方案2】:

如果使用正则表达式,请尝试:

>>> import re
>>> text = '<tr><td class="yfnc_tablehead1" width="74%">Diluted EPS (ttm):</td><
td class="yfnc_tabledata1">7.58</td>"'
>>> re.findall('Diluted\s+EPS\s+\(ttm\).*?>([\d.]+)<', text)
['7.58']

更新这是使用requests和re的示例代码:

import requests
import re

sess = requests.Session()
res = sess.get('http://finance.yahoo.com/q/ks?s=MMM+Key+Statistics')
print re.findall('Diluted\s+EPS\s+\(ttm\).*?>([\d.]+)<', res.text)

输出:

[u'7.58']

【讨论】:

  • 谢谢!我会试一试,然后告诉你进展如何。
  • 如何在不将实际数字放入“text =”行的情况下执行此操作?我需要它来动态更新,它将被合并到针对不同股票代码运行的其他东西中。尝试使用此代码时,我也收到“扫描字符串文字时 EOL”错误。
  • 谢谢,我会试用那个版本。我不知道 3.68 是从哪里来的。正确的结果是 7.58。我能够通过修复上面的解决方案来获得它。我的代码现在看起来像这样... sess = requests.Session() res = sess.get('finance.yahoo.com/q/ks?s=MMM+Key+Statistics') soup = BeautifulSoup(res.text, 'html.parser') eps = soup.find('td' , text='稀释 EPS (ttm):').parent.find('td', attrs={'class': 'yfnc_tabledata1'}) for i in eps: print (i)
  • 对此感到抱歉。我更新了模式。请尝试:print re.findall('Diluted\s+EPS\s+\(ttm\).*?&gt;([\d.]+)&lt;', res.text).
  • 如果使用正则表达式,则不需要 soup = BeautifulSoup(res.text, 'html.parser')。请查看更新后的代码。
【解决方案3】:

感谢您回答我的问题。我能够使用两种方法来获得所需的值。第一种方式是这样的。

from bs4 import BeautifulSoup
import requests

sess = requests.Session()
res = sess.get('http://finance.yahoo.com/q/ks?s=MMM+Key+Statistics')

soup = BeautifulSoup(res.text, 'html.parser')

eps = soup.find('td', text='Diluted EPS (ttm):').parent.find('td', attrs={'class': 'yfnc_tabledata1'})

for i in eps:
    print (i)

这是第二种方式...

import requests
import re

sess = requests.Session()
res = sess.get('http://finance.yahoo.com/q/ks?s=MMM+Key+Statistics')
print (re.findall('Diluted\s+EPS\s+\(ttm\).*?>([\d.]+)<', res.text.strip()))

我还没有完全理解这一切,但这是一个很好的开始,有两种不同的方式来理解它并继续整合项目的这一方面。非常感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多