【发布时间】:2018-08-02 02:22:38
【问题描述】:
大家早上好
我最近开始使用 BeautifulSoup 并观看视频并阅读它,目的是每天从网络上抓取股价信息并将其添加到先前填充的包含历史股价的 csv 文件中。
我已经尝试对我的代码(如下)进行多次修改,无论我是使用“div”还是“span”元素,然后添加完整的类名 - 我最终都会收到空括号“[]”作为我的打印在控制台中。
我使用的网站是 yahoo Finance - 所以我尝试使用另一个网站 Sharenet,同样的问题。然后我尝试抓取网站的另一部分(仅限共享名称) - 也是空括号。我收到结果的唯一一次是当我抓取一个嵌套了多个项目的“div”时 - 在打印输出中我可以看到股价信息,但肯定有办法只获得价格吗?
我一直在 youtube 上使用以下视频作为指南,该视频与之前在此处发布的类似问题的帖子一起非常有帮助,但我仍然遇到问题。
https://www.youtube.com/watch?v=XQgXKtPSzUI
import yahoo finance stock price with beautifulsoup and request
以下是我的代码(我使用的是 python 2.7):
import urllib2
from bs4 import BeautifulSoup as soup
#Opens the connection and downloads the webpage
kio_site = urllib2.urlopen("https://finance.yahoo.com/quote/KIO.JO?p=KIO.JO")
#This will print all the html on the webpage
kio_html = kio_site.read()
#Now closing the internet connection that you opened before
kio_site.close()
#now you want to parse the html file
page_soup = soup(kio_html, "html.parser")
#Specifically find certain elements
kio_info = page_soup.find_all("span", {"class":"Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)"})
print kio_info
当我改用下面的代码时,我得到了一个结果,但股价却在混乱之中:
kio_info = page_soup.find_all("div", {"class":"My(6px) smartphone_Mt(15px)"})
在打印输出中,我还看到在股价数字之前有一个“data-reactid”=“14”,但即使我将它包含在我的代码中(以及“span”和“class”“Trsdu (0.3s)”等)它也没有给我价格。
难道我阅读网页的方式不应该是html?我尝试使用 lxml 但出现错误。
提前感谢您的帮助!
【问题讨论】:
标签: python beautifulsoup