【问题标题】:How do I correctly parse data using JSoup (java)如何使用 JSoup (java) 正确解析数据
【发布时间】:2014-09-09 07:43:21
【问题描述】:

我想使用 JSoup (java) 解析出这个 HTML(CompanyName、Location、jobDescription...)中的数据。我在尝试迭代作业列表时卡住了

HTML 中的提取是我想要迭代并从中提取数据的许多“JOBLISTING”div 之一。我只是无法处理如何迭代特定的 div 对象。抱歉这个菜鸟问题,但也许有人可以帮助我,他们已经知道要使用哪个功能。选择?

<div class="between_listings"><!-- local.spacer --></div>

<div id="joblisting-2944914" class="joblisting listing-even listing-even company-98028 " itemscope itemtype="http://schema.org/JobPosting">


<div class="company_logo" itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization">
     <a href="/stellenangebote-des-unternehmens--Delivery-Hero-Holding-GmbH--98028.html" title="Jobs Delivery Hero Holding GmbH" itemprop="url">
       <img src="/upload_de/logo/D/logoDelivery-Hero-Holding-GmbH-98028DE.gif" alt="Logo Delivery Hero Holding GmbH" itemprop="image" width="160" height="80" />
     </a>
</div>


<div class="job_info">


<div class="h3 job_title">
   <a id="jobtitle-2944914" href="/stellenangebote--Junior-Business-Intelligence-Analyst-CRM-m-f-Berlin-Delivery-Hero-Holding-GmbH--2944914-inline.html?ssaPOP=204&ssaPOR=203" title="Arbeiten bei Delivery Hero Holding GmbH" itemprop="url">
      <span itemprop="title">Junior Business Intelligence Analyst / CRM (m/f)</span>
   </a>
</div>

<div class="h3 company_name" itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization">

    <span itemprop="name">Delivery Hero Holding GmbH</span>

</div>

</div>




<div class="job_location_date">

    <div class="job_location target-location">
         <div class="job_location_info" itemprop="jobLocation" itemscope itemtype="http://schema.org/Place">


            <div class="h3 locality" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
                  <span itemprop="addressLocality"> Berlin</span>
            </div>


            <span class="location_actions">
                <a href="javaScript:PopUp('http://www.stepstone.de/5/standort.html?OfferId=2944914&ssaPOP=203&ssaPOR=203','resultList',800,520,1)" class="action_showlistingonmap showlabel" title="Google Maps" itemprop="maps">
                   <span class="location-icon"><!-- --></span>
                   <span class="location-label">Google Maps</span>
                </a>
            </span>

          </div>
       </div>

       <div class="job_date_added" itemprop="datePosted"><time datetime="2014-07-04">04.07.14</time></div>
</div>


<div class="job_actions">


</div>

</div>
<div class="between_listings"><!-- local.spacer --></div>

文件输入 = new File("C:/Talend/workspace/WEBCRAWLER/output/keywords_SOA.txt"); // 加载文件到extraction1 Document ParseResult = Jsoup.parse(input, "UTF-8", "http://example.com/");元素 jobListingElements = ParseResult.select(".joblisting"); for (元素 jobListingElement: jobListingElements) { jobListingElement.select(".companyName span[itemprop=\"name\"]"); // 其他元素属性 System.out.println(jobListingElements);

Java 代码:

File input = new File("C:/Talend/workspace/WEBCRAWLER/output/keywords_SOA.txt");
// Load file into extraction1       
Document ParseResult = Jsoup.parse(input, "UTF-8", "http://example.com/");                          
Elements jobListingElements = ParseResult.select(".joblisting");        
for (Element jobListingElement: jobListingElements) {         
    jobListingElement.select(".companyName span[itemprop=\"name\"]");         
    // other element properties         
    System.out.println(jobListingElements);
}

谢谢!

【问题讨论】:

  • 欢迎来到 SO。您能否在问题中包含您尝试过的代码?
  • 抱歉,我无法正确格式化。感谢您的热烈欢迎。 File input = new File("C:/Talend/workspace/WEBCRAWLER/output/keywords_SOA.txt"); // Load file into extraction1 Document ParseResult = Jsoup.parse(input, "UTF-8", "http://example.com/"); Elements jobListingElements = ParseResult.select(".joblisting"); for (Element jobListingElement: jobListingElements) { jobListingElement.select(".companyName span[itemprop=\"name\"]"); // other element properties System.out.println(jobListingElements);
  • 我不明白为什么这不起作用。 Elements jobListingElements = ParseResult.select(".joblisting"); for (Element jobListingElement: jobListingElements) { Elements e1 = jobListingElement.select(".companyName span[itemprop=\"name\"]"); // other element properties System.out.println(e1.text());

标签: java parsing jsoup


【解决方案1】:

所以你的 Jsoup 文档对吗?如果 css 类 joblisting 没有出现在其他任何地方,这似乎很容易。

Document document = Jsoup.parse(new File("d:/bla.html"), "utf-8");
Elements elements = document.select(".joblisting");
for (Element element : elements) {
    Elements jobTitleElement = element.select(".job_title span");
    Elements companyNameElement = element.select(".company_name spanspan[itemprop=name]");
    String companyName = companyNameElement.text();
    String jobTitle = jobTitleElement.text();

    System.out.println(companyName);
    System.out.println(jobTitle);
}

不知道为什么属性[itemprop*=\"name\"]选择器找不到span(延伸阅读:http://jsoup.org/cookbook/extracting-data/selector-syntax

知道了:span[itemprop=name] 没有任何引号或转义。其他属性或值也应该能够获得更具体的选择。

【讨论】:

  • 非常感谢您的快速答复!我提前为我的缓慢学习道歉,但让我向你解释一下我的目标。
  • 也许你可以帮我举一个具体的例子,它会产生一个输出,我可以从中看到要走的路。先感谢您!你帮了我很多!
  • 它似乎已经吞下了我的解释。我想提取特定变量(例如 Company_Name、Job Title 等)并将它们导入 SQL-DB(稍后)。你能给我一个如何使用你的代码的例子吗? span[itemprop=\"name\"] 例如,我不明白那部分。我试过这个,这对我来说看起来很愚蠢=)File input = new Elements jobListingElements = ParseResult.select(".joblisting"); for (Element jobListingElement: jobListingElements) { jobListingElement.select(".companyName span[itemprop=\"name\"]"); System.out.println(jobListingElements);
  • 更新了我的答案.. 不要使用属性选择器。我没有尝试就写了答案。不知何故,jsoup 找不到具有属性的跨度。
  • 像魅力一样工作,谢谢。现在我必须弄清楚如何将它写入我的本地 MS-SQL Server。我想我会在几个小时内回来,哈哈。我马上就喜欢上了这个页面。不过,还不能给你一个赞成票
猜你喜欢
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 1970-01-01
  • 2019-04-29
相关资源
最近更新 更多