Reputation: 493
I am trying to extract text data from HTML tags using rvest
.
Data:
[vc_row css_animation="" row_type="row" use_row_as_full_screen_section="no" type="full_width" angled_section="no" text_align="left" background_image_as_pattern="without_pattern"][vc_column][vc_column_text]\n\\n<h6 class="button" style="padding: 0px 42%;">Description</h6>\n\\n<ol>\n\\n \t<li>Ideal for : Women</li>\n\\n \t<li>Package Contents : 1 Pcs</li>\n\\n \t<li>Fit Type : Regular, Relaxed, Classic and Slim Fit.</li>\n\\n \t<li>Care Instructions : Machine Wash and Normal Wash.</li>\n\\n \t<li>Occasion : Lough, Smart, Dressy, Business, Casual and Formal.</li>\n\\n \t<li>Sleeve Type : Short sleeves, 3/4th Sleeves, Full Sleeves ,Kimono Sleeves and Off Shoulder.</li>\n\\n \t<li>Browse our Brand In Love for more choices of Shrugs , Tops and T shirt and Western wear Collections.</li>\n\\n \t<li>Care Instructions : Ensure washing the tee in cold water, don't iron directly on the print and don't dry in direct sunlight.</li>\n\\n</ol>\n\\n<img class="alignnone wp-image-858" src="https://justelite.in/wp-content/uploads/2020/06/art-size.jpg" alt="" />\n\\n<h6 class="button" style="padding: 0px 42%;">Reviews</h6>\n\\n[/vc_column_text][/vc_column][/vc_row]
What I did is:
html_text(read_html(as.character(data)))
Still I get vc_row css_animation
and some other tag which is not removed.
dput
data:
structure(2L, .Label = c("", "[vc_row css_animation=\"\" row_type=\"row\" use_row_as_full_screen_section=\"no\" type=\"full_width\" angled_section=\"no\" text_align=\"left\" background_image_as_pattern=\"without_pattern\"][vc_column][vc_column_text]\n\\n<h6 class=\"button\" style=\"padding: 0px 42%;\">Description</h6>\n\\n<ol>\n\\n \t<li>Ideal for : Women</li>\n\\n \t<li>Package Contents : 1 Pcs</li>\n\\n \t<li>Fit Type : Regular, Relaxed, Classic and Slim Fit.</li>\n\\n \t<li>Care Instructions : Machine Wash and Normal Wash.</li>\n\\n \t<li>Occasion : Lough, Smart, Dressy, Business, Casual and Formal.</li>\n\\n \t<li>Sleeve Type : Short sleeves, 3/4th Sleeves, Full Sleeves ,Kimono Sleeves and Off Shoulder.</li>\n\\n \t<li>Browse our Brand In Love for more choices of Shrugs , Tops and T shirt and Western wear Collections.</li>\n\\n \t<li>Care Instructions : Ensure washing the tee in cold water, don't iron directly on the print and don't dry in direct sunlight.</li>\n\\n</ol>\n\\n<img class=\"alignnone wp-image-858\" src=\"https://justelite.in/wp-content/uploads/2020/06/art-size.jpg\" alt=\"\" />\n\\n<h6 class=\"button\" style=\"padding: 0px 42%;\">Reviews</h6>\n\\n[/vc_column_text][/vc_column][/vc_row]"
), class = "factor")
Upvotes: 0
Views: 232
Reputation: 12420
What you've got there are not proper html tags as far as I can see since these are normally demarcated by "<" and ">" (e.g., < h1 >
). Yours are surrounded by [ h1 ]
. Adapting the function linked above, you could do:
s <- structure(2L, .Label = c("", "[vc_row css_animation=\"\" row_type=\"row\" use_row_as_full_screen_section=\"no\" type=\"full_width\" angled_section=\"no\" text_align=\"left\" background_image_as_pattern=\"without_pattern\"][vc_column][vc_column_text]\n\\n<h6 class=\"button\" style=\"padding: 0px 42%;\">Description</h6>\n\\n<ol>\n\\n \t<li>Ideal for : Women</li>\n\\n \t<li>Package Contents : 1 Pcs</li>\n\\n \t<li>Fit Type : Regular, Relaxed, Classic and Slim Fit.</li>\n\\n \t<li>Care Instructions : Machine Wash and Normal Wash.</li>\n\\n \t<li>Occasion : Lough, Smart, Dressy, Business, Casual and Formal.</li>\n\\n \t<li>Sleeve Type : Short sleeves, 3/4th Sleeves, Full Sleeves ,Kimono Sleeves and Off Shoulder.</li>\n\\n \t<li>Browse our Brand In Love for more choices of Shrugs , Tops and T shirt and Western wear Collections.</li>\n\\n \t<li>Care Instructions : Ensure washing the tee in cold water, don't iron directly on the print and don't dry in direct sunlight.</li>\n\\n</ol>\n\\n<img class=\"alignnone wp-image-858\" src=\"https://justelite.in/wp-content/uploads/2020/06/art-size.jpg\" alt=\"\" />\n\\n<h6 class=\"button\" style=\"padding: 0px 42%;\">Reviews</h6>\n\\n[/vc_column_text][/vc_column][/vc_row]"
), class = "factor")
cleanFun <- function(htmlString) {
return(gsub("<.*?>|\\[.*?\\]", "", htmlString))
}
cleanFun(s)
#> [1] "\n\\nDescription\n\\n\n\\n \tIdeal for : Women\n\\n \tPackage Contents : 1 Pcs\n\\n \tFit Type : Regular, Relaxed, Classic and Slim Fit.\n\\n \tCare Instructions : Machine Wash and Normal Wash.\n\\n \tOccasion : Lough, Smart, Dressy, Business, Casual and Formal.\n\\n \tSleeve Type : Short sleeves, 3/4th Sleeves, Full Sleeves ,Kimono Sleeves and Off Shoulder.\n\\n \tBrowse our Brand In Love for more choices of Shrugs , Tops and T shirt and Western wear Collections.\n\\n \tCare Instructions : Ensure washing the tee in cold water, don't iron directly on the print and don't dry in direct sunlight.\n\\n\n\\n\n\\nReviews\n\\n"
Created on 2020-09-16 by the reprex package (v0.3.0)
Upvotes: 1