所以我最初在几天前发布请求帮助开发一个加载网页并从有序列表标签中过滤出所需内容的项目。
一位用户非常有帮助地向我提供了如何使用 Jsoup 来做到这一点的方法,如下所示。
public static String pharmercyURL = "http://archiveofourown.org/tags/Fareeha%20%22Pharah%22%20Amari*s*Angela%20%22Mercy%22%20Ziegler/works";
Document doc = Jsoup.connect(pharmercyURL).get();
Elements ol = doc.select("ol.work > li");
for (Element li : ol) {
String title = li.select("h4.heading a").first().text();
String author = li.select("h4.heading a[rel=author]").text();
String id = li.attr("id").replaceAll("work_","");
String url = "http://archiveofourown.com/works/" + id;
String summary = li.select("blockquote.summary").text();
String rating = li.select("span.rating").text();
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("ID: " + id);
System.out.println("URL: " + url);
System.out.println("Summary: " + summary);
System.out.println("Rating: " + rating);
}
虽然它在我的家用电脑上运行得非常好,但我也想在我的 discord 服务器上实现它,我的机器人的其余部分是用 C# 编写的。
幸运的是,事实证明 Csquery 具有相同的处理方式,使用 CSS 选择器和诸如此类的东西来过滤数据。但是,我在将 Java 代码转换为正确的 C# 时遇到了一些问题,主要是因为我使用了一组单独的库,并且不完全确定 CsQuery 背后的框架。
错误:
Unable to cast object of type 'CsQuery.Implementation.HTMLLIElement' to type 'CsQuery.CQ'.
-
String searchURL = "http://archiveofourown.org/works/search?utf8=✓&work_search%5Bquery%5D=";
searchURL += fandomParam + "+" + pairingParam + "+" + titleParam + "+" + authorParam + "+" + ratingParam + "+" + otherParam;
CQ dom = CQ.CreateFromUrl(searchURL);
CQ ol = dom.Select("ol.work > li");
Console.WriteLine("sucessfully past CQ initialization");
foreach(CQ li in ol){
String title = li.Select("h4.heading a").First().Text();
await ReplyAsync(title);
break;
}