![](/img/trans.png)
[英]PHP: How to find and extract an element with src attribute in html (from url)
[英]PHP: How to find an element with particular name attribute in html (from url)
我目前正在使用PHP的file_get_contents($url)
從URL中獲取內容。 獲取內容后,我需要檢查給定的HTML塊,找到具有給定name屬性的'select'
,提取其選項以及它們的值文本。 我不確定該怎么做,我可以使用PHP的simplehtmldom類來解析html,但是如何獲得名稱為“ union”的特定“ select”
<span class="d3-box">
<select name='union' class="blockInput" >
<option value="">Select a option</option> ..
頁面可以有多個“選擇”框,因此我需要按名稱屬性專門查看
<?php
include_once("simple_html_dom.php");
$htmlContent = file_get_contents($url);
foreach($htmlContent->find(byname['union']) as $element)
echo 'option : value';
?>
任何幫助表示贊賞。 先感謝您。
試試這個PHP代碼:
<?php
require_once dirname(__FILE__) . "/simple_html_dom.php";
$url = "Your link here";
$htmlContent = str_get_html(file_get_contents($url));
foreach ($htmlContent->find("select[name='union'] option") as $element) {
$option = $element->plaintext;
$value = $element->getAttribute("value");
echo $option . ":" . $value . "<br>";
}
?>
這個怎么樣:
$htmlContent = file_get_html('your url');
$htmlContent->find('select[name= "union"]');
以面向對象的方式:
$html = new simple_html_dom();
$htmlContent = $html->load_file('your url');
$htmlContent->find('select[name= "union"]');
從DOMDocument文檔中: http : //www.php.net/manual/zh/class.domdocument.php
$html = file_get_contents( $url );
$dom = new DOMDocument();
$dom->loadHTML( $html );
$selects = $dom->getElementsByTagName( 'select' );
$select = $selects->item(0);
// Assuming all children are options.
$children = $select->childNodes;
$options_values = array();
for ( $i = 0; $i < $children->length; $i++ )
{
$item = $children->item( $i );
$options_values[] = $item->nodeValue;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.