【问题标题】:How to find XML on webpages如何在网页上查找 XML
【发布时间】:2015-07-03 23:16:11
【问题描述】:

对于以下网页: http://www.realclearpolitics.com/epolls/2012/president/us/general_election_romney_vs_obama-1171.html

我将如何查找与图表中的数据关联的 XML。我知道 XML 页面是 http://charts.realclearpolitics.com/charts/1171.xml,因为有人告诉我。但是我自己怎么解决这个问题呢?

谢谢

【问题讨论】:

  • 没有规则说从 XML 生成的每个内容也需要公开 XML。
  • 美国政治 - 三个 Gs - Guns Gays 和 God!。看起来很清楚

标签: xml screen-scraping webpage


【解决方案1】:

通常你可以在网站上找到一个rss链接或者查看页面源并搜索.xml

您也可以在 google 上使用以下查询:

网站:realclearpolitics.com 文件类型:xml

【讨论】:

    【解决方案2】:

    您是否尝试过以下 linq xml?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("http://charts.realclearpolitics.com/charts/1171.xml");
    
            }
        }
    }
    ​
    

    【讨论】:

      【解决方案3】:

      终于搞定了

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Windows.Forms.DataVisualization.Charting;
      using System.Xml;
      using System.Xml.Linq;
      using System.IO;
      
      namespace WindowsFormsApplication1
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
                  XDocument doc = XDocument.Load("http://charts.realclearpolitics.com/charts/1171.xml");
                  var results = new {
                      X = doc.Descendants("series").Descendants("value").Select(y => (DateTime)y).ToList(),
                      Obama = doc.Descendants("graph").Where(y => y.Attribute("title").Value == "Obama").Descendants("value").Select(y => (string)y == "" ?null : (double?)y).ToList(),
                      Romney = doc.Descendants("graph").Where(y => y.Attribute("title").Value == "Romney").Descendants("value").Select(y => (string)y == "" ?null : (double?)y).ToList()
                  };
      
      
                  chart1.Series.Add("Obama");
                  chart1.Series["Obama"].ChartType = SeriesChartType.Point;
                  chart1.Series["Obama"].Points.DataBindXY(results.X, results.Obama);
                   chart1.Series.Add("Romney");
                  chart1.Series["Romney"].ChartType = SeriesChartType.Point;
                  chart1.Series["Romney"].Points.DataBindXY(results.X, results.Romney);
                  chart1.DataBind();
      
              }
          }
      }
      ​
      

      【讨论】:

        猜你喜欢
        • 2022-01-14
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多