【问题标题】:Is there a Method to read the Content of a Website?是否有读取网站内容的方法?
【发布时间】:2020-07-08 11:01:04
【问题描述】:

我想编写一个程序来读取在线日历,将其与数据库中的名称进行比较,并以某种方式使用此数据。但如果我使用 WebClient,它会读取网站的源代码,而不是内容。这是我的代码:

using System;
using System.Diagnostics;
using System.Net;
using MySql.Data.MySqlClient;
namespace CalendarCrawler
{
    class Program
    {

        static void KillTask(string Task)
        {
            Process[] Process = new Process[] { };
            Process = Process.GetProcessesByName(Task);
            foreach (Process Instance in Process)
            {
                Instance.Kill();
            }

            
        }
        static String ReadContent(String Website)
        {
            WebClient web = new WebClient();
            System.IO.Stream stream = web.OpenRead(Website);
            using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))
            {
                String text = reader.ReadToEnd();
                return text;
            }

        }

        static void Main(string[] args)

        {
            Console.WriteLine("Getting Connection ...");

            var datasource = "localhost";//your server

            var database = "database"; //your database name
            var username = "username"; //username of server to connect
            var password = "password"; //password

            //your connection string 
            string connStr = $"Server={datasource};Database={database};Uid={username};Pwd={password}";
            //create instanace of database connection
            using (var conn = new MySqlConnection(connStr))
            {
                try
                {
                    Console.WriteLine("Openning Connection ...");

                    //open connection
                    conn.Open();

                    Console.WriteLine("Connection successful!");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
                String Websitetext = ReadContent("http://www.esel.at/termine");
                var stm = $"INSERT INTO content(Content) VALUES (@1);";
                var cmd = new MySqlCommand(stm, conn);
                cmd.Parameters.AddWithValue("@1", Websitetext);
                cmd.ExecuteNonQuery();
               
                Console.WriteLine(Websitetext);
                KillTask("CalendarCrawler");
            }
        }
    }
}

Killtask 方法只是从后台进程中清除它,因此构建新版本没有问题。 我希望有人可以帮助我。

【问题讨论】:

  • 我建议使用较新的 HttpClient 来做 http 请求。源是内容。您必须做的是解析源代码,并且有很多 libearies。
  • 看看 HtmlAgilityPack:html-agility-pack.net
  • 这里的“源代码”与“内容”是什么意思?它只是要读取 HTML,确定吗?那的内容...
  • 一旦涉及 JavaScript,使用WebClient 检索网站内容很可能会遇到障碍。如果可能,不惜一切代价避免网络抓取并使用任何可用的 API。否则,请使用 HtmlAgilityPack。
  • 网站好像有一个API。例如,它发送 url 为“esel.at/api/termine/data?date=08.07.2020&selection=false”的 get 请求,请求返回 JSON。也许您应该尝试发送相同的请求?

标签: c# automation web-crawler webclient


【解决方案1】:

实际上有 2 个不同的网络用户。人和电脑。人们喜欢按钮和表格 (UI) 等闪亮的东西,计算机更喜欢 XML 或 JSON (API) 之类的东西。

您的日历网站有一个 UI,这是您当前看到的(在浏览器中和“下载代码”时)。它可能也有一个 API,这就是你应该在你的程序中使用的。

我刚刚快速浏览了esel.at,它似乎没有(公共)API(但这可能是因为 Google 无法正确翻译该页面)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-06
    • 2013-06-15
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    相关资源
    最近更新 更多