1

I am new in Web Scraping. I have one HTML page from which I am looking to scrape data. I have managed getting data like title etc using below code

$name= $html->find('h1[class=page-title]', 0)->plaintext;

But Now I have one div like below

<div class="company--performance-data">

          <div class="field field--name-field-stock-price-prev-close field--type-decimal field--label-above field__items">
      <div class="field__label">Previous trading day’s Close</div>
                <div content="61.58000" class="field__item">
        $61.58
            </div>
          </div>

          <div class="field field--name-field-stock-price-current-high field--type-decimal field--label-above field__items">
      <div class="field__label">Current high for the day</div>
                <div content="64.40000" class="field__item">
        $64.40
            </div>
          </div>

          <div class="field field--name-field-stock-price-current-low field--type-decimal field--label-above field__items">
      <div class="field__label">Current low for the day</div>
                <div content="62.78000" class="field__item">
        $62.78
            </div>
          </div>

          <div class="field field--name-field-days-volume field--type-decimal field--label-above field__items">
      <div class="field__label">Day's Volume</div>
                <div class="field__item">
        406
            </div>
          </div>

      
          <div class="field field--name-field-30-days-average-volume field--type-decimal field--label-above field__items">
      <div class="field__label">30 days average volume</div>
                <div content="0.553734" class="field__item">
        1
            </div>
          </div>

          <div class="field field--name-field-market-cap field--type-string field--label-above field__items">
      <div class="field__label">Market Cap</div>
                <div class="field__item">
        $0.00
            </div>
          </div>

          <div class="field field--name-field-stock-52-week-high field--type-decimal field--label-above field__items">
      <div class="field__label">52-week high</div>
                <div content="72.60" class="field__item">
        $72.60
            </div>
          </div>

          <div class="field field--name-field-stock-52-week-low field--type-decimal field--label-above field__items">
      <div class="field__label">52-week low</div>
                <div content="42.64" class="field__item">
        $42.64
            </div>
          </div>

          <div class="field field--name-field-stock-last-paid-div-value field--type-decimal field--label-above field__items">
      <div class="field__label">Last paid dividend value</div>
                <div content="0.47" class="field__item">
        $0.47
            </div>
          </div>

          <div class="field field--name-field-stock-dividend-yield field--type-decimal field--label-above field__items">
      <div class="field__label">Dividend Yield</div>
                <div content="3.03814" class="field__item">
        3.04%
            </div>
          </div>

          <div class="field field--name-field-stock-funds-from-operation field--type-decimal field--label-above field__items">
      <div class="field__label">Previous quarter FFO</div>
                <div content="217.76" class="field__item">
        $217.76mil
            </div>
          </div>


      </div>

I want get each field value like $0.47 from it. But since its have class name field, I am not getting idea how I can get value from it. Anyone here can please help me for same?

Thanks a lot!

3
  • Is there always a <div content="..."> containing each value? If yes, why don't you get it directly? Commented Feb 16, 2021 at 8:43
  • @AbsoluteBeginner as You can see some div have not that value. Commented Feb 16, 2021 at 9:21
  • See here: stackoverflow.com/questions/59447744/… Commented Feb 16, 2021 at 9:29

1 Answer 1

0

Try this please:

foreach($html1->find("div.field__item") as $m)
{
    $txt = trim($m->plaintext);
    $attribute_array = $m->attr;
    if(isset($attribute_array['content']))
    {
        var_dump($attribute_array['content']);
    }
    var_dump($txt);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi! its nice but some div have not content=. Can you please expend answer for it? Thanks!
So you want the values of content="62.78000" also? i.e., the value 62.78000?
Hi! all that div have values, however some have not content so instead we can use values. Thanks!
For this div: <div content="64.40000" class="field__item"> $64.40 </div> do you want to pull $64.40 or 64.40000?
Edited the answer. Please check.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.