解析表内容

时间:2019-12-23 05:29:46

标签: c# web-scraping html-agility-pack

我很难弄清楚为什么它不起作用。 URL是这样的: https://www.cmlviz.com/pivot-points/GOOG

我想用复选框分析表,它的类名是“可更新”,ID为“ pivot_data_grid”。我究竟做错了什么?这真让我抓狂!我只想获取表中每一行的值以及标题(例如:5天指数移动平均线)。谢谢您的帮助

        string url = "https://www.cmlviz.com/pivot-points/{symbol}".Replace("{symbol}", symbol);

        HtmlDocument htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(url);
        //var nodes = htmlDoc.DocumentNode.SelectNodes("//div[@id=pivot_data_grid]/table");
        var table = htmlDoc.DocumentNode.SelectNodes("//div[@class='updatable']");
        var table = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='updatable']/table");

1 个答案:

答案 0 :(得分:1)

from bs4 import BeautifulSoup
import requests

r = requests.get(
    "https://www.cmlviz.com/inc/pivot-points.php?ticker=GOOG&key=57f42fb7382e7ef5679277fb3c2431c1")


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

for item in soup.findAll("div", {'class': 'updatable'}):
    print(item.get_text("\n", strip=True))

输出:

Alphabet Inc Real-time Moving Average Pivot Points
Current Price
$
1,351.22
-1.27 Fibonacci Extension
$711.74
52 Week Low
$977.66
Select MAs for your chart:
5-day Exponential Moving Average
$
1,351.99
8-day Exponential Moving Average
$
1,349.83
10-day Exponential Moving Average
$
1,347.71
13-day Exponential Moving Average
$
1,344.21
21-day Exponential Moving Average
$
1,334.60
50-day Simple Moving Average
$
1,300.28
200-day Simple Moving Average
$
1,206.62
.382 Fibonacci Retracement
$1,216.62
.50 Fibonacci Retracement
$1,171.00
.618 Fibonacci Retracement
$1,125.37
52 Week High
$1364.33
1.27 Fibonacci Extension
$1,735.43
RSI 20
63.20