【问题标题】:Scraping Yahoo Earning Calendar抓取雅虎收入日历
【发布时间】:2016-03-09 01:48:09
【问题描述】:

我是 Python 编程新手。我正在尝试获取符号和时间。

我能够获得适合我的时间。它只出现第一个词,有时是一个时间,而不是“之后/之前”市场关闭。

但是当涉及到符号时,我不想要任何外国市场,所以没有 .??在符号中。这是我到目前为止所拥有的。对不起,如果它有点草率。这是我在 python 中的第一个真正的程序....

import requests
import urllib2
import re
from bs4 import BeautifulSoup

site= "http://www.nasdaq.com/earnings/report/acrx"
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8'}

url = "http://biz.yahoo.com/research/earncal/20150309.html"

content = urllib2.urlopen(url).read()

soup = BeautifulSoup(content)

m = re.findall('center><small>\S+ ', content)
w = re.findall('\?s=\w+',content)

x=0
lp = (len(m))
xlp = lp -1


for x in range (xlp):
    print x, m[x+1][14:], w[x][3:]

【问题讨论】:

    标签: python web-scraping yahoo-finance


    【解决方案1】:

    您可以使用BeautifulSoup 来解析HTML,而不是使用正则表达式。您也可以使用time 库来获取当前日期。

    这是一个用于抓取 yahoo Finance 的示例代码。

    import requests
    import bs4
    
    def get_earning_data(date):
        html = requests.get("https://biz.yahoo.com/research/earncal/{}.html".format(date), headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0"}).text
        soup = bs4.BeautifulSoup(html)
        quotes = []
        for tr in soup.find_all("tr"):
            if len(tr.contents) > 3:
                if len(tr.contents[1].contents) > 0:
                    if tr.contents[1].contents[0].name == "a":
                        if tr.contents[1].contents[0]["href"].startswith("http://finance.yahoo.com/q?s="):
                            quotes.append({     "name"  : tr.contents[0].text
                                               ,"symbol": tr.contents[1].contents[0].text
                                               ,"url"   : tr.contents[1].contents[0]["href"]
                                               ,"eps"   : tr.contents[2].text if len(tr.contents) == 6 else u'N/A'
                                               ,"time"  : tr.contents[3].text if len(tr.contents) == 6 else tr.contents[2].text
                                           })
        return quotes
    
    print(get_earning_data("20150309"))
    

    【讨论】:

    • 仍然有效!你刚刚为我写了我下午的代码。干杯,伙计!
    猜你喜欢
    • 2018-04-21
    • 2019-10-28
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 2023-03-27
    • 2020-04-01
    相关资源
    最近更新 更多