Web data parsing simple question

Discussion in 'PHP' started by pcz, May 29, 2011.

  1. #1
    I am creating my first web data parsing script.
    I understand that you have to look at the source code and take the info you need from there. But what if the div tags are the same?

    For example:
    <div id="1">random number 1</div>...some other code....<div id="1">random number 2</div>

    I need only random number 2, however both have the <div id ="1">. How can I use the code above but print only the "random number 2"
     
    pcz, May 29, 2011 IP
  2. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #2
    It would be easier to see the script your wrote, but here is a shot in the dark, hoping that I understand you right:

    <?php
    $random = rand(1,10);
    echo '<div id="1">Random number: ' . $random . '</div>';
    ?>
    PHP:
     
    DomainerHelper, May 29, 2011 IP
  3. Minimal Hank

    Minimal Hank Peon

    Messages:
    136
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The second element of your preg array is the one you are looking for. Where's the problem ?
     
    Minimal Hank, May 29, 2011 IP
  4. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #4
    There is no mention about a preg array.
     
    DomainerHelper, May 29, 2011 IP
  5. chanif.alfath

    chanif.alfath Active Member

    Messages:
    148
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    #5
    maybe you can use looping,
    and only print on the equal of "random number 2".

    let me know your script,
    so maybe i can help you to check it :)
     
    chanif.alfath, May 31, 2011 IP
  6. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #6
    I am guessing the thread was abondoned.

    If you want this random, a loop is overkill just do as I showed:

     
    DomainerHelper, May 31, 2011 IP
  7. chanif.alfath

    chanif.alfath Active Member

    Messages:
    148
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    88
    #7
    your script will only output random number between 0-10.
    what i mean when using loop, is using it on the inner div,
    and using it with the pattern that he want to use,
    maybe like :
    0..2..4..6..8 or
    0..5..10..15..

    that what i mean :)
    thanks,

     
    chanif.alfath, May 31, 2011 IP
  8. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #8
    A loop is good if you don't want X showing 5 times before Y finally shows, I give you that for sure.

    But, I don't hink it matters, PCZ has not replied to anyone yet. lol
     
    DomainerHelper, May 31, 2011 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    He's talking about scrapping an external web-page.
     
    MyVodaFone, May 31, 2011 IP
  10. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #10
    Ah, that makes more sense!

    In this case, I would look at the tags surrounding the div tags in question. Do you have a better more full example?
     
    DomainerHelper, May 31, 2011 IP
  11. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #11
    The tags are the same, so he's going to use preg_match_all and echo out or store match[0][1] as a variable
     
    MyVodaFone, May 31, 2011 IP
  12. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #12
    I know those tags are the same. However, you can more effectively import data for the purpose of storing in SQL by looking at the tags outside of those tags. For example:

    
    <div style="float:left;clear:left">
              <div class="title">Random Number 1</div>
              <div id="1">random number 1</div>
    </div>
    <div style="float:left;clear:right">
              <div class="title">Random Number 2</div>
              <div id="1">random number 2</div>
    </div>
    
    
    HTML:
    In this case I can grab #1 separately while skipping any that he does not want, by including the container tags as well. Better than just grabbing all of the values off a page indiscriminately, in other words more control. But I could be complicating it more than actually needed, I just like to be very precise.
     
    DomainerHelper, May 31, 2011 IP
  13. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #13
    
    <?php
    $text = '<div style="float:left;clear:left">
              <div class="title">Random Number 1</div>
              <div id="1">123456</div>
    </div>
    <div style="float:left;clear:right">
              <div class="title">Random Number 2</div>
              <div id="1">654321</div>
    </div>';
    
    
    preg_match_all('#<div id="1">([0-9]+)</div>#', $text, $match_random);
    
    $random1 = $match_random[0][0]; // 123456
    $random2 = $match_random[0][1]; // 654321
    
    echo $random2;
    
    ?>
    
    PHP:
     
    MyVodaFone, May 31, 2011 IP
  14. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #14
    Good, but what if you want to skip XX item without putting a loop into the mix? that is what I mean.
     
    DomainerHelper, May 31, 2011 IP
  15. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #15
    Sorry I'm not with you... the OP wants to out-put
     
    MyVodaFone, May 31, 2011 IP
  16. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #16
    Misread it, my bad, long day:)
     
    DomainerHelper, May 31, 2011 IP
  17. m0nster

    m0nster Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    preg_match("#<div id="1">(.*)</div>#", $your_output, $matches);
    print_r($matches);
    
    PHP:
    with this it will print out your array of matches, if you need the second number you just use $matches[1]
     
    m0nster, Jun 2, 2011 IP