如何在使用 jSoup 进行网页抓取时找到正确的选择器?

huangapple 未分类评论45阅读模式
英文:

How do I find the right selector when scraping a website with jSoup?

问题

String url = "https://www.centralcharts.com/en/price-list-ranking/ALL/asc/ts_29-us-nyse-stocks--qc_1-alphabetical-order";
Document doc = Jsoup.connect(url).userAgent("Jsoup Scraper").get();

String stock = "tr:nth-of-type(1) > .footable-first-visible";
Elements stockName = doc.select(stock);
List<String> stocks = new ArrayList<String>();

for (Element e : stockName) 
{
    stocks.add(e.text());
}

for (String s : stocks) {
    System.out.println(s);
}

Please let me know if you need further assistance.

英文:

I am currently learning how to web scrape and am using jSoup to do so. I read the jSoup cookbook on how to do so and followed it's steps. I made sure the source code for the website prints, making sure that is working and is correct. But when I have to find a selector for looping through a websites table, I don't know what to look for, or what to select. I will leave my code below, though I just would like to know what to look for.

    String url = &quot;https://www.centralcharts.com/en/price-list-ranking/ALL/asc/ts_29-us-nyse-stocks--qc_1-alphabetical-order&quot;;
    Document doc = Jsoup.connect(url).userAgent(&quot;Jsoup Scraper&quot;).get();

    String stock = &quot;tr:nth-of-type(1) &gt; .footable-first-visible&quot;;
    Elements stockName = doc.select(stock);
    List&lt;String&gt; stocks = new ArrayList&lt;String&gt;();
    
    for (Element e : stockName) 
    {
        stocks.add(e.text());
    }

    for (String s : stocks) {
        System.out.println(s);
    }

huangapple
  • 本文由 发表于 2020年4月10日 21:27:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/61141292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定