【问题标题】:Unable to pick content of class tag in web scraping无法在网络抓取中选择类标签的内容
【发布时间】:2021-03-30 10:22:44
【问题描述】:

我正在使用 JSOUP 学习网络 scraping,我一直在尝试检索类标签 sc-fzqBZW sc-fzoyAV eGbWXV 的数据。我尝试用点替换空格,但似乎没有任何效果。我在下面附上代码块。如果有人指出我的错误并为我纠正,我将非常感激。 这是行:

"

需要accountId"
public static void main(String[] args) throws Exception {
        System.out.println("Loading Scraper!");
        final String url = "https://apidocs.temenos.com/service/account-management#operation/getAccountClosure";

        Document doc;
        try {
            doc = Jsoup.connect(url).get();
            String title = doc.title();
            System.out.println("Title: " + title);
            org.jsoup.select.Elements elements = doc.select("sc-fzqBZW.sc-fzoyAV.eGbWXV");
//original class tag = sc-fzqBZW sc-fzoyAV eGbWXV

            for (Element element : elements) {
                System.out.println("Ticker:" + element.text());

            }

        } catch (Exception ex) {
            System.out.println("Error in establising connection. /n" + "Error: " + ex);
        }

    } 

【问题讨论】:

  • 仅供参考,它是 scrape(和 scrapescrapedscraper)不是报废

标签: java web-scraping jsoup


【解决方案1】:

根据此处的文档https://jsoup.org/cookbook/extracting-data/selector-syntax

你可以试试

 doc.select("td.sc-fzqBZW sc-fzoyAV eGbWXV");

【讨论】:

  • 那也没用。我调试了代码,结果发现这没有进入 for 循环,因此崩溃了。
【解决方案2】:

您的选择器sc-fzqBZW.sc-fzoyAV.eGbWXV 开头缺少点,表示sc-fzqBZW 是一个类。现在它将匹配这样的元素:

<sc-fzqBZW class="sc-fzoyAV eGbWXV">

使用.sc-fzqBZW.sc-fzoyAV.eGbWXV

【讨论】:

    【解决方案3】:
    for (int i = 0; i < elements.size(); i++) {
    
       //it will return "accountId" text
    
       elements.get(i).getElementsByTag("span").get(1).html();
    
    
       //it will return "required" text
    
       elements.get(i).getElementsByClass("sc-fzoWqW sc-fzplgP eDnpFZ").first().html();
     
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多