Jsoup解析:将动态值解析为键值对

时间:2014-01-07 15:49:58

标签: java jsoup

我从url(http://www.gmdu.net/corp-902113.html)中提取这些数据..任何人都可以帮我提取以下格式的数据..指导我请

<div class="content">
    <div class="label">Company Name: </div>
        Haycolour (Pvt) Ltd
    <br/>
    <div class="label">Business Owner: </div>
        Hayleys (Group of Companies
    <br/>
    <div class="label">Employees: </div>
        50
    <br/>
    <div class="label">Main markets: </div>
        Asia and south Asia
    <br/>
    <div class="label">Business Type: </div>
        Manufacturer and Importers
    <br/>
    <div class="label">Product/Service: </div>
    <br/>
    <div class="label">About Us: </div>
    Hayley’s is one of 
    <a rel="tag" title="Sri Lanka Manufacturers" href="http://www.gmdu.net/tags-Sri-Lanka-p1.html">Sri Lanka</a>
    ’s largest multinational business conglomerates with a history spanning 135 years. Commencing commercial    operations in 1878 as Chas. P. Hayley and Company incorporated as Hayley’s Limited 61 years ago. We drive a     globally competitive business portfolio encompassing 12 defined sectors of enterprise cutting across    manufacturing, Industrial solutions, agriculture and services etc. www.Hayleys.com
    <br/>
    Haycolour (Pvt) Ltd is one of the largest manufactures for 
    <a rel="tag" title="Pigment Emulsion Manufacturers" href="http://www.gmdu.net/tags-Pigment-Emulsion-    p1.html">Pigment Emulsion</a>
    in Textile printing Industry in Sri Lanka, Garment panel printing, Latex, Inks and water base paints. We supply to  local Textile Printing industry and also export to Asian region, such as Bangladesh, Thailand, Malaysia,    Philippines and Pakistan and are open to the rest of the world’s market. Haycolour (Pvt) Limited is a fully owned   subsidiary of Hayley’s PLC, Sri Lankas premier blue chip conglomerate. www.haycolour.com , (Site under  construction)
    <br/>
<div class="vcard">
    <div class="label">Category: </div>
    <span class="category">
    <a title="Chemicals Manufacturers" href="http://www.gmdu.net/list-5-p1.html">Chemicals</a>
    - 
    <a title="Textile Chemicals Manufacturers" href="http://www.gmdu.net/list-5-273-p1.html">Textile Chemicals</a>
    </span>
    <br/>
    <div class="label">Region: </div>
    <span class="adr country-name">
    <a title="Sri Lanka Manufacturers" href="http://www.gmdu.net/loca-35-p1.html">Sri Lanka</a>
    </span>
    <span class="fn org">Haycolour (Pvt) Ltd</span>
    <br/>
    <div class="label">Link Tool: </div>
    <a title="Sri Lanka Chemicals Manufacturers" href="http://www.gmdu.net/join-5-join-35-p1.html">Sri Lanka    Chemicals</a>
     - 
    <a title="Sri Lanka Textile Chemicals Manufacturers" href="http://www.gmdu.net/join-5-273-join-35-p1.html">Sri  Lanka Textile Chemicals</a>
        <br/>
    <div class="label">Tags: </div>
    <span class="tags">
    <a rel="tag" title="Hayleys Limited Manufacturers" href="http://www.gmdu.net/tags-Hayleys-Limited-p1.html">Hayleys  Limited</a>
     - 
    <a rel="tag" title="Sri Lanka Manufacturers" href="http://www.gmdu.net/tags-Sri-Lanka-p1.html">Sri Lanka</a>
    - 
    <a rel="tag" title="P Hayley Manufacturers" href="http://www.gmdu.net/tags-P-Hayley-p1.html">P Hayley</a>
    - 
    <a rel="tag" title="Pigment Emulsion Manufacturers" href="http://www.gmdu.net/tags-Pigment-Emulsion-    p1.html">Pigment Emulsion</a>
    </span>
    <br/>
    <div class="label">Contact Person: </div>
    Farooq Abbas
    <br/>
<div class="label">Zip Code: </div>
<span class="adr postal-code"/>
<br/>
<div class="label">Website: </div>
<span class="url">www.hayleys.com</span>
<br/>
<div class="label">Address: </div>
<span class="adr street-address">No.25,Foster Lane,Colombo 10,</span>
<br/>
<div class="label">Telephone: </div>
<span class="tel">0094779123026</span>
<br/>
<div class="label">Fax: </div>
<span class="fax">0094112689139</span>
<br/>
<div class="label">PDF: </div>
<a type="application/pdf" rel="alternate" title="Haycolour (Pvt) Ltd PDF" href="http://www.gmdu.net/corp-902113.pdf">Haycolour (Pvt) Ltd PDF</a>
<br/>
<div class="label">Google Map: </div>
<a title="Haycolour (Pvt) Ltd Map" href="http://www.gmdu.net/map-902113.html">Haycolour (Pvt) Ltd Map</a>
</div>
</div>

我需要将价值追溯为

Company Name:Haycolour (Pvt) Ltd
Business Owner:Hayleys (Group of Companies
Employees:50
Main markets:Asia and south Asia
Business Type:Manufacturer and Importers
Product/Service:

About Us:Hayley’s is one of Sri Lanka’s largest multinational business conglomerates with a history spanning 135 years. Commencing commercial operations in 1878 as Chas. P. Hayley and Company incorporated as Hayley’s Limited 61 years ago. We drive a globally competitive business portfolio encompassing 12 defined sectors of enterprise cutting across manufacturing, Industrial solutions, agriculture and services etc. www.Hayleys.com
Category:Chemicals - Textile Chemicals
Region:Sri Lanka Haycolour (Pvt) Ltd
Link Tool:Sri Lanka Chemicals - Sri Lanka Textile Chemicals
Tags:Hayleys Limited - Sri Lanka - P Hayley - Pigment Emulsion
Contact Person:Farooq Abbas
Zip Code:

Website:www.hayleys.com
Address:No.25,Foster Lane,Colombo 10,
Telephone:0094779123026
Fax:0094112689139
PDF:Haycolour (Pvt) Ltd PDF
Google Map:Haycolour (Pvt) Ltd Map

我的代码:

public static void main(String[] args) throws IOException {
        Document doc = Jsoup.parse(input);
        Elements labels = doc.select("div.content div.label");
        for (Element label : labels) {
            System.out.println(String.format("%s:%s", label.text().trim(),
                    label.nextSibling().outerHtml()));
        }
    }

我的输出:

Company Name:Haycolour (Pvt) Ltd
Business Owner:Hayleys (Group of Companies
Employees:50
Main markets:Asia and south Asia
Business Type:Manufacturer and Importers
Product/Service:

About Us:Hayley’s is one of Sri Lanka’s
Website::<span class="url">www.hayleys.com</span> Address::<span class="adr street-address">No.25,Foster Lane,Colombo 10,</span> Telephone::<span class="tel">0094779123026</span>

这里添加了一些span类..请指导我

1 个答案:

答案 0 :(得分:0)

String url = "http://www.gmdu.net/corp-902113.html";

Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
                    .get();

Element content = doc.select("div.content").get(0);

doc = Jsoup.parse( content.html().replaceAll("</div>", "</div><span>").replaceAll("<div", "</span><div") );

Elements labels = doc.select("div.label");
for (Element label : labels) {
    System.out.println(String.format("%s %s", label.text().trim(),
            label.nextElementSibling().text()));
}

输出

Company Name: Haycolour (Pvt) Ltd
Business Owner: Hayleys (Group of Companies
Employees: 50
Main markets: Asia and south Asia
Business Type: Manufacturer and Importers
Product/Service:
About Us: Hayley’s is one of Sri Lanka’s largest multinational business conglomerates with a history spanning 135 years. Commencing commercial operations in 1878 as Chas. P. Hayley and Company incorporated as Hayley’s Limited 61 years ago. We drive a globally competitive business portfolio encompassing 12 defined sectors of enterprise cutting across manufacturing, Industrial solutions, agriculture and services etc. www.Hayleys.com Haycolour (Pvt) Ltd is one of the largest manufactures for Pigment Emulsion in Textile printing Industry in Sri Lanka, Garment panel printing, Latex, Inks and water base paints. We supply to local Textile Printing industry and also export to Asian region, such as Bangladesh, Thailand, Malaysia, Philippines and Pakistan and are open to the rest of the world’s market. Haycolour (Pvt) Limited is a fully owned subsidiary of Hayley’s PLC, Sri Lankas premier blue chip conglomerate. www.haycolour.com , (Site under construction) Haycolour Products are certified by prestigious Hohenstein Institute of Germany for Eco Passport, and Haycolour is proud to be the first Sri Lankan company to be certified and this proves the stint quality assurance procedures that we have instituted to make our offerings internationally competitive. As an advantage derived through this certification, any Sri-Lankan Supplier/our customer who applies for Okeo Tex 100 certification can use our products and there is no need to test our pigments for Okeo Tex compliances. Eco Passport is worldwide certification on the chemical compliance of production of human ecological optimized textiles. Our product are compliances with free of APEO, AZO, Formaldehyde, Phthalates, heavy metals etc. And our pigment emulsions are certified to use on baby Garments which is another significant factor. As a South Asian country manufacturing Pigment Emulsions to be used in Textile and garments exports to EU and America, Haycolour has so many barriers to overcome against the multinational chemical manufactures and competitors in India, Taiwan, Korea and China. But the strategies implemented by the company have helped to capture and remain in Export markets. Our main strength is consistency in quality. We never compromise our quality. This is well accepted by our buyers. Also prompt deliveries and competitive prices are two another major factors which help Haycolour to be strong. Haycolour provides entire solutions to the textile printers in Sri-Lanka and abroad, who cater to the export garment industry. The service includes all the products required for T-shirt printing industry with different types of rubber pastes for cotton, cotton blends and Nylon fabrics, Gillter, Flocks Discharge paste, Crack Binders, Foils, 3D AND SPECIALEFFECTCHEMICALS. Haycolour also provides total solutions to Textile and Garment dyeing industry by representing leading giants in Reactive, Disperse and Nylon dyes in the world market. Thanking you and awaiting your favorable response to start a good and long lasting business relationship with you.
Category: Chemicals - Textile Chemicals
Region: Sri Lanka Haycolour (Pvt) Ltd
Link Tool: Sri Lanka Chemicals - Sri Lanka Textile Chemicals
Tags: Hayleys Limited - Sri Lanka - P Hayley - Pigment Emulsion
Contact Person: Farooq Abbas
Zip Code:
Website: www.hayleys.com
Address: No.25,Foster Lane,Colombo 10,
Telephone: 0094779123026
Fax: 0094112689139
PDF: Haycolour (Pvt) Ltd PDF
Google Map: Haycolour (Pvt) Ltd Map

从网站的移动版本解析

String url = "http://www.gmdu.net/corp-902113.html";

Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")
                    .get();

Elements units = doc.select("div.unit");
System.out.println("Company Name: " + units.get(1).getElementsByTag("h1").get(0).text());
System.out.println("About Us: " + units.get(2).select("div.text").get(0).text());
for(int i = 1; i < 4; i++) {
    Elements elements = units.get(i).getElementsByTag("li");
    for(Element element : elements) 
        System.out.println(element.text());
}

输出

Company Name: Haycolour (Pvt) Ltd
About Us: Hayley’s is one of Sri Lanka’s largest multinational business conglomerates with a history spanning 135 years. Commencing commercial operations in 1878 as Chas. P. Hayley and Company incorporated as Hayley’s Limited 61 years ago. We drive a globally competitive business portfolio encompassing 12 defined sectors of enterprise cutting across manufacturing, Industrial solutions, agriculture and services etc. www.Hayleys.com Haycolour (Pvt) Ltd is one of the largest manufactures for Pigment Emulsion in Textile printing Industry in Sri Lanka, Garment panel printing, Latex, Inks and water base paints. We supply to local Textile Printing industry and also export to Asian region, such as Bangladesh, Thailand, Malaysia, Philippines and Pakistan and are open to the rest of the world’s market. Haycolour (Pvt) Limited is a fully owned subsidiary of Hayley’s PLC, Sri Lankas premier blue chip conglomerate. www.haycolour.com , (Site under construction) Haycolour Products are certified by prestigious Hohenstein Institute of Germany for Eco Passport, and Haycolour is proud to be the first Sri Lankan company to be certified and this proves the stint quality assurance procedures that we have instituted to make our offerings internationally competitive. As an advantage derived through this certification, any Sri-Lankan Supplier/our customer who applies for Okeo Tex 100 certification can use our products and there is no need to test our pigments for Okeo Tex compliances. Eco Passport is worldwide certification on the chemical compliance of production of human ecological optimized textiles. Our product are compliances with free of APEO, AZO, Formaldehyde, Phthalates, heavy metals etc. And our pigment emulsions are certified to use on baby Garments which is another significant factor. As a South Asian country manufacturing Pigment Emulsions to be used in Textile and garments exports to EU and America, Haycolour has so many barriers to overcome against the multinational chemical manufactures and competitors in India, Taiwan, Korea and China. But the strategies implemented by the company have helped to capture and remain in Export markets. Our main strength is consistency in quality. We never compromise our quality. This is well accepted by our buyers. Also prompt deliveries and competitive prices are two another major factors which help Haycolour to be strong. Haycolour provides entire solutions to the textile printers in Sri-Lanka and abroad, who cater to the export garment industry. The service includes all the products required for T-shirt printing industry with different types of rubber pastes for cotton, cotton blends and Nylon fabrics, Gillter, Flocks Discharge paste, Crack Binders, Foils, 3D AND SPECIALEFFECTCHEMICALS. Haycolour also provides total solutions to Textile and Garment dyeing industry by representing leading giants in Reactive, Disperse and Nylon dyes in the world market. Thanking you and awaiting your favorable response to start a good and long lasting business relationship with you.
Owner: Hayleys (Group of Companies
Employees: 50
Markets: Asia and south Asia
Business: Manufacturer and Importers
Product:
Category: Chemicals - Textile Chemicals
Link Tool: Sri Lanka Chemicals - Sri Lanka Textile Chemicals
Tags: Hayleys Limited - Sri Lanka - P Hayley - Pigment Emulsion
Contact: Farooq Abbas
Website: www.hayleys.com
Telephone: 0094779123026
Fax: 0094112689139
Region: Sri Lanka
Zip Code:
Address: No.25,Foster Lane,Colombo 10,
Google Map: Haycolour (Pvt) Ltd Map