如果 div class 出现在 id 之后,如何提取数据?

How to extract data if the div class comes after an id?

我尝试从 div 获取一些数据,这些数据嵌入在 ID 和 type=hidden 之后。我无法到达 class 以获取 class 中列出的链接。

我将 Jsoup 与 Elements 和 .select() 或 .getElementsbyId() 结合使用,并尝试将它们组合起来以达到 class。没有成功。该站点是 https://www.ariva.de/aktien/suche。如果您点击搜索 "Suche starten" 按钮,结果 table 会弹出。在这个 table 中,链接是我想要达到的。

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class DatenImportUnternehmen {

 public static void main (String[] args) {

  String url = "https://www.ariva.de/aktien/suche";


  try {

   Document document = Jsoup.connect(url).get();

   for (Element row : document.select("div.aktiensuche_result_table")) {
    if(row.select("input[type=hidden]").text().equals("")) {
     continue;
    }
    else {
     String raw = row.select("[type=hidden]").text();
     System.out.println(raw);

   }
   }
   }
   catch (Exception ex) {
    ex.printStackTrace();
   }
}       

我没有得到任何结果。 Eclipse 只是声明已终止。

如果我没理解错的话,您想访问 https://www.ariva.de/aktien/suche 上的搜索按钮时生成的 table 中的链接。

您遇到的第一个问题是无法直接从此 URL 获得搜索结果。相反,当您单击搜索按钮时,会向 https://www.ariva.de/aktiensuche/_result_table.m 发出 POST 请求 该请求的结果实际上包含 table 以及我相信您感兴趣的链接。具体而言,响应包含 HTML,然后将其作为结果 table 动态添加到页面。

第二个问题看起来是在 jsoup 查询中。我在结果 table 中看不到任何隐藏的输入字段,但是使用 document.select("a[href]").

很容易获取链接

所以对我来说这个代码:

String searchUrl = "https://www.ariva.de/aktiensuche/_result_table.m";
String searchBody = "page=0&page_size=25&sort=ariva_name&sort_d=asc&ariva_performance_1_year=_&ariva_performance_3_years=&ariva_performance_5_years=&index=0&founding_year=&land=0&industrial_sector=0&sector=0&currency=0&type_of_share=0&year=_all_years&sales=_&profit_loss=&sum_assets=&sum_liabilities=&number_of_shares=&earnings_per_share=&dividend_per_share=&turnover_per_share=&book_value_per_share=&cashflow_per_share=&balance_sheet_total_per_share=&number_of_employees=&turnover_per_employee=_&profit_per_employee=&kgv=_&kuv=_&kbv=_&dividend_yield=_&return_on_sales=_";

// post request to search URL
Document document = Jsoup.connect(searchUrl).requestBody(searchBody).post();

// find links in returned HTML
for(Element link:document.select("a[href]")) {
    System.out.println(link);
}

产生输出:

<a href="/1-1_drillisch-aktie">1&amp;1 Drillisch</a>
<a href="/11_88_0_solutions-aktie">11 88 0 Solutions</a>
<a href="/1st_red-aktie">1st Red</a>
<a href="/21st-_cent-_fox_b_new-aktie">21ST. CENT. FOX B NEW</a>
<a href="/21st_century_fox-aktie">21st Century Fox</a>
<a href="/2g_energy-aktie">2G Energy</a>
<a href="/3i_group-aktie">3I Group</a>
<a href="/3i_infrastructure-aktie">3I INFRASTRUCTURE</a>
<a href="/3m_company-aktie">3M Company</a>
<a href="/3u_holding-aktie">3U Holding</a>
<a href="/3w_power-aktie">3W Power</a>
<a href="/4imprint_group-aktie">4imprint Group</a>
<a href="/4_sc-aktie">4 SC</a>
<a href="/XS0421565150">6,625% Statkraft AS 09/19 auf Festzins</a>
<a href="/7c_solarparken-aktie">7C Solarparken</a>
<a href="/888_holdings-aktie">888 Holdings</a>
<a href="/a-a-a-_aktiengesellschaft_allgemeine_anlageverwaltung-aktie">A.A.A. aktiengesellschaft allgemeine anlageverwaltung</a>
<a href="/a-g-_barr_______ls-04167-aktie">A.G. BARR LS-,04167</a>
<a href="/a-h-t-_syngas_technology-aktie">A.H.T. Syngas Technology</a>
<a href="/a-s-_creation_tapeten-aktie">A.S. Creation Tapeten</a>
<a href="/a-j_mucklow_group-1-aktie">A+J Mucklow Group</a>
<a href="/a-jmucklow_grp_pref-_ls_1-aktie">A+JMUCKLOW GRP PREF. LS 1</a>
<a href="/a2a-aktie">A2A</a>
<a href="/aac_technologies_holding-aktie">AAC Technologies Holding</a>
<a href="/aalberts-aktie">Aalberts</a>

我希望这或多或少是您所追求的。要设置搜索参数,您需要检查搜索表单并修改 searchBody 字符串中的表单数据(或使用 .data 方法而不是 .requestBody 来构建查询)。