【问题标题】:Search Number Range on Website in Python用 Python 在网站上搜索号码范围
【发布时间】:2017-05-16 04:27:29
【问题描述】:

以下是在网站上搜索单词的脚本。在这种情况下,它是https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC 上的“S&P”。

我的问题是如何搜索 2300 到 2400 之间的数字范围......或小于/大于 2400 的数字。基本上,我正在制作一个脚本,告诉我价格何时达到某个点.

感谢您的帮助!

import webbrowser
import urllib.request

page = urllib.request.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")
content = page.read().decode('utf-8')
if "S&P" in content :
    webbrowser.open("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")

2017 年 5 月 16 日更新 下面的人帮助了我。感谢并感谢所有回复。我修修补补并在下面做了。

import urllib.request
import webbrowser
from bs4 import BeautifulSoup
page = urllib.request.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")
content = page.read().decode('utf-8')
soup = BeautifulSoup(content, 'html.parser')
val = soup.find("span", {"data-reactid": "36"}).decode_contents(formatter="html")
if val >= "2,400.00":
    webbrowser.open("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")

【问题讨论】:

    标签: python-3.x if-statement numbers range


    【解决方案1】:

    使用 BeautifulSoup 尝试以下操作:

    >>> import urllib2
    >>> from bs4 import BeautifulSoup
    >>> page = urllib2.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")
    >>> page = urllib2.urlopen("https://finance.yahoo.com/quote/%5EGSPC?p=^GSPC")
    >>> content = page.read().decode('utf-8')
    >>> soup = BeautifulSoup(content, 'html.parser')
    >>> val = soup.find("span", {"data-reactid": "36"}).decode_contents(formatter="html")
    >>> val
    u'2,402.32'
    >>>
    

    然后,转换为浮点数以检查它是否与您的断点匹配:

    >>> val = float(val.replace(',',''))
    >>> val
    2402.32
    >>> 
    

    【讨论】:

      【解决方案2】:

      您可以使用 BS4 的美汤。使用 pip 安装。

      from bs4 import BeautifulSoup as soup
      content=soup(content,'lxml')
      stuffs=content.findAll(class_="Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")
      #stuffs will contain your stock price. you can check what range its in and stuff with it
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-12
        • 2010-11-23
        • 2022-01-09
        • 2011-08-24
        相关资源
        最近更新 更多