如何使用BeautifulSoup刮刮雅虎收益日历

时间:2019-06-14 00:11:23

标签: python html web-scraping beautifulsoup

如何刮取Yahoo收入日历以提取日期?

这是针对python 3的。

from bs4 import BeautifulSoup as soup
import urllib

url = 'https://finance.yahoo.com/calendar/earnings?day=2019-06-13&symbol=ibm'

response = urllib.request.urlopen(url)
html = response.read()

page_soup = soup(html,'lxml')
table = page_soup.find('p')
print(table)

输出为“无”

3 个答案:

答案 0 :(得分:1)

Beautiful Soup提供了一些查找功能,可用于检查DOM,请参考documentation

from bs4 import BeautifulSoup as soup
import urllib.request

url = 'https://finance.yahoo.com/calendar/earnings?day=2019-06-13&symbol=ibm'

response = urllib.request.urlopen(url)
html = response.read()

page_soup = soup(html,'lxml')
table = page_soup.find_all('td')
Dates = []
for something in table:
    try:
        if something['aria-label'] == "Earnings Date":
            Dates.append(something.text)
    except:
        print('')

print(Dates)

答案 1 :(得分:1)

可能不合时宜,但是由于您希望从网页中获取表格,因此您可以考虑使用可使用两行代码的熊猫:

import pandas as pd
earnings = pd.read_html('https://finance.yahoo.com/calendar/earnings?day=2019-06-13&symbol=ibm')[0]

答案 2 :(得分:0)

这是两种简洁的方法

import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://finance.yahoo.com/calendar/earnings?day=2019-06-13&symbol=ibm&guccounter=1')
soup = bs(r.content, 'lxml')
# using attribute = value selector
dates = [td.text for td in soup.select('[aria-label="Earnings Date"]')]
#using nth-of-type to get column
dates = [td.text for td in soup.select('#cal-res-table td:nth-of-type(3)')]