带有简单 html dom 解析器的垃圾 Javascript 和 Css 代码
Junk Javascript and Css code with simple html dom parser
我正在使用简单的 html dom 解析器来解析 link 和 php。在我正在使用的 url 和 php 代码下方。
URL:
https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html
PHP 脚本:
$html = file_get_html('https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html');
foreach($html->find('div#ProductDescription_Tab') as $description)
{
$comments = $description->find('.hsn_comments', 0);
$comments->outertext = '';
print $description->outertext ;
}
问题是,在 运行 脚本之后,我得到了我想要的前端,但是查看页面源代码显示了很多 javascript 和 css 垃圾代码。可以吗?我不能只得到 html 标签而不需要任何额外的 css 或 javascript 代码吗?下面是 php 脚本查看页面源代码在 运行 脚本之后的图像。
如果您使用的是最新版本的simpleHTMLDom,您可以使用remove()
功能。这是基于您现有代码的示例代码
$html = file_get_html('https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html');
foreach($html->find('div#ProductDescription_Tab') as $description)
{
$comments = $description->find('.hsn_comments', 0);
$comments->outertext = '';
//remove div with script
$description->find('div#flix-minisite',0)->remove();
$description->find('div#flix-inpage',0)->remove();
//will remove all <script> tags
foreach($description->find('script') as $s) $s->remove();
//wil remove all <style> tags
foreach($description->find('style') as $s) $s->remove();
echo $description->innertext ;
}
我正在使用简单的 html dom 解析器来解析 link 和 php。在我正在使用的 url 和 php 代码下方。
URL:
https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html
PHP 脚本:
$html = file_get_html('https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html');
foreach($html->find('div#ProductDescription_Tab') as $description)
{
$comments = $description->find('.hsn_comments', 0);
$comments->outertext = '';
print $description->outertext ;
}
问题是,在 运行 脚本之后,我得到了我想要的前端,但是查看页面源代码显示了很多 javascript 和 css 垃圾代码。可以吗?我不能只得到 html 标签而不需要任何额外的 css 或 javascript 代码吗?下面是 php 脚本查看页面源代码在 运行 脚本之后的图像。
如果您使用的是最新版本的simpleHTMLDom,您可以使用remove()
功能。这是基于您现有代码的示例代码
$html = file_get_html('https://homeshopping.pk/products/-Imported-Stretchable-Tights-For-Women--Pack-Of-3-.html');
foreach($html->find('div#ProductDescription_Tab') as $description)
{
$comments = $description->find('.hsn_comments', 0);
$comments->outertext = '';
//remove div with script
$description->find('div#flix-minisite',0)->remove();
$description->find('div#flix-inpage',0)->remove();
//will remove all <script> tags
foreach($description->find('script') as $s) $s->remove();
//wil remove all <style> tags
foreach($description->find('style') as $s) $s->remove();
echo $description->innertext ;
}