【问题标题】:Jsoup Scraping HTML dynamic contentJsoup 抓取 HTML 动态内容
【发布时间】:2016-09-04 13:17:03
【问题描述】:

我是 Jsoup 的新手,我一直在尝试创建一个小代码,使用 Jsoup 获取 Steam 库存中的项目名称。

public Element getItem(String user) throws IOException{
    Document doc;

    doc = Jsoup.connect("http://steamcommunity.com/id/"+user+"/inventory").get();
    Element element = doc.getElementsByClass("hover_item_name").first();
    return element;
}

此方法返回:

<h1 class="hover_item_name" id="iteminfo0_item_name"></h1>

并且我想要在您单击特定窗口时生成的“h1”标签之间的信息。 先感谢您。

【问题讨论】:

  • 你的意思是“

    XYZ

    ”->“XYZ”?
  • 是的,我想要那个“XYZ”,但它是在您单击特定窗口时生成的

标签: java html css jsoup


【解决方案1】:

使用.text() 并返回String,即:

public String getItem(String user) throws IOException{
    Document doc;
    doc = Jsoup.connect("http://steamcommunity.com/id/"+user+"/inventory").get();
    Element element = doc.getElementsByClass("hover_item_name").first();
    String text = element.text();
    return text;
}

【讨论】:

    【解决方案2】:

    你可以使用.select(String cssQuery)方法:

    doc.select("h1")给你所有h1Elements。 如果您需要这些标签中的实际文本,请为每个 Element 使用 .text()。 如果您需要classid 之类的属性,请在Element 上使用.attr(String attributeKey),例如:

    doc.getElementsByClass("hover_item_name").first().attr("id")
    

    给你"iteminfo0_item_name"

    但是,如果您需要在网站上执行点击,您无法使用 JSoup 执行此操作,因此 JSoup 是一种 HTML 解析器,而不是替代浏览器。 Jsoup 无法处理动态内容。

    但你可以做的是,首先在你的h1标签中抓取相关数据,然后发送一个新的.post()request,分别发送一个ajax call

    如果您想要一个真正的网络驱动程序,请查看Selenium

    【讨论】:

    • 谢谢,问题是我想处理动态内容,所以我认为我会使用与 Jsoup 不同的另一种方法
    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2015-08-17
    • 2019-01-13
    相关资源
    最近更新 更多