【问题标题】:Only scrape specific details from a web page仅从网页中抓取特定详细信息
【发布时间】:2016-04-23 14:17:36
【问题描述】:

我正在使用 Jsoup 从网页中检索详细信息并写入文本文件。我可以只检索其中的一部分吗?例如在以下链接中,我只想获取职位描述。

http://aldogroup.luceosolutions.com/recruit/stores/advert_details.php?id=3136&_lang=en&partner_id=139

有时职位发布来自不同的网站,因此 html 标签的格式可能会有所不同。我需要一种仅检索职位描述的方法。以下代码检索网页上的所有内容。我怎样才能只得到职位描述?请帮忙。

public class MainCollector {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Document doc;
        try {
            doc = Jsoup.connect("http://aldogroup.luceosolutions.com/recruit/stores/advert_details.php?id=3136&_lang=en&partner_id=139").get();
            String title = doc.title();
            String body = doc.body().toString();
            Document convertText = Jsoup.parseBodyFragment(body);
            String convertedText = convertText.text();
            System.out.println("Title:" + title);
            System.out.println("Body:" + convertedText);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}  

【问题讨论】:

    标签: java html web-scraping jsoup webpage


    【解决方案1】:

    你可以用这个 -

    Elements e = doc.select(".annonce > p:nth-child(5)");
    System.out.println(e.text());
    

    要获得正确的CSS selector,您可以打开浏览器的开发者工具(按 F12),然后选择检查器工具。
    您还应该将user agent 字符串添加到您的请求中,这样您就可以从浏览器和程序中获得完全相同的页面 -

    doc = Jsoup.connect("http://aldogroup.luceosolutions.com/recruit/stores/advert_details.php?id=3136&_lang=en&partner_id=139")
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")
                    .get();
    

    【讨论】:

    • 对于这个页面:sjobs.brassring.com/TGWebHost/… 我只想获取 div 标签“PrimaryContentBlock”中的内容。如何在代码中指定?
    • 嗯,要获取该特定标签的内容,您可以使用Elements e = doc.select("#PrimaryContentBlock");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多