簡體   English   中英

使用HtmlAgilityPack進行Web爬網Java站點

[英]Web Scraping Java Sites with HtmlAgilityPack

首先,我要說我不是網絡抓取專家。 我可以在大多數平台上進行基礎操作,僅此而已。

我正在嘗試為Web應用程序創建基礎,該應用程序可以通過生成其他數據,指標以及創建用於自我測試的新工具來幫助用戶加強其語言學習。 Duolingo網站沒有提供任何類型的API,所以我現在的下一個想法就是刪除https://www.duome.eu/ 我寫了一個快速的小刮板,但沒有意識到該站點是Java。 在以下示例中,我希望從“ Words選項卡中收集所有包含錨點的Words

using System;
using HtmlAgilityPack;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace DuolingoUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "Podus";
            UpdateDuolingoUser(userName);
            Console.ReadLine();
        }

        private static async void UpdateDuolingoUser(string userName)
        {
            string url = "https://www.duome.eu/" + userName + "/progress/";

            // Create the http client connection
            HttpClient httpClient = new HttpClient();
            var html = await httpClient.GetStringAsync(url);

            // Store the html client data in an object
            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);

            //var words = htmlDocument.DocumentNode.Descendants("div")
            //    .Where(node => node.GetAttributeValue("id", "")
            //    .Equals("words")).ToList();

            //var wordList = words[0].Descendants("a")
            //    .Where(node => node.GetAttributeValue("class", "")
            //    .Contains("wA")).ToList();

            Console.WriteLine(html);
        }
    }
}

上面代碼的html對象包含:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="google" value="notranslate">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Duolingo · Podus @ duome.eu</title>
<link rel="stylesheet" href="/style.css?1548418871" />
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        if("".length==0){
            var visitortime = new Date();
            var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
            //localStorage.tz = visitortimezone;
            //timezone = Date.parse(localStorage.tz);
            //timezone = localStorage.tz;
            //console.log(timezone);
            $.ajax({
                type: "GET",
                url: "/tz.php",
                data: 'time='+ visitortimezone,
                success: function(){
                    location.reload();
                }
            });
        }
    });
</script>

</head>
<body>
<noscript>Click <a href="https://duome.eu//Podus/progress/">here</a> to adjsut XP charts to your local timezone. </noscript>
<!-- Yandex.Metrika counter --> <script type="text/javascript" > (function (d, w, c) { (w[c] = w[c] || []).push(function() { try { w.yaCounter47765476 = new Ya.Metrika({ id:47765476, clickmap:true, trackLinks:true, accurateTrackBounce:true }); } catch(e) { } }); var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function () { n.parentNode.insertBefore(s, n); }; s.type = "text/javascript"; s.async = true; s.src = "https://mc.yandex.ru/metrika/watch.js"; if (w.opera == "[object Opera]") { d.addEventListener("DOMContentLoaded", f, false); } else { f(); } })(document, window, "yandex_metrika_callbacks"); </script> <noscript><div><img src="https://mc.yandex.ru/watch/47765476" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- /Yandex.Metrika counter -->
</body>
</html>

但是,如果您轉到實際的URL https://www.duome.eu/Podus/progress/ ,則該站點將包含大量腳本。 因此,在檢查時,第一個問題是我沒有獲得在瀏覽器中看到的html。 第二個問題是,如果您查看源代碼,它的內容與inspect內容完全不同,並且我在源代碼中看不到任何內容,這會導致我將數據與div id="words"隔離開來。

鑒於我對Java構建的網頁缺乏了解,我該怎么做,或者有可能嗎?

您可以通過https://www.duolingo.com/users/<username>以JSON格式訪問Dualingo配置文件數據

例如。 https://www.duolingo.com/users/Podus

這比嘗試手動抓取duome配置文件頁面要容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM