开发者

Use PHP to extract simple numeric data from website and display as HTML

开发者 https://www.devze.com 2023-01-24 19:16 出处:网络
I have no clue at all. How do I extract the numeric % data on the right from the link below and display them on my website without updating daily myself? Can a simple PHP + HTML solve my problem?

I have no clue at all.

How do I extract the numeric % data on the right from the link below and display them on my website without updating daily myself? Can a simple PHP + HTML solve my problem?

http://www.mrrebates.com/merchants/all_merchants.asp

Meanwhile, how do I automatically hyperlink the extracted numeric % and display it as a link for that retailer? for example,

1 Stop Florists------------------------- 8% (this 8% should be dis开发者_开发知识库played as hyperlink for that retailer, unfortunately I am too new to have more than 1 hyperlink)

at the same time integrating my referral id (shown below) on to that 8% hyperlink mrrebates.com?refid=420149


You can use curl to download the page, then use regular expressions to parse it up and print it out in whatever form you want. Here's some PHP code to do it:

<?php
system("curl -v http://www.mrrebates.com/merchants/all_merchants.asp > /tmp/x.txt");
$data = file_get_contents("/tmp/x.txt");

preg_match_all('/<td><a href="([^"]*)".*?<b>([^<]*)<\/b>.*?<td class="r">([^<]*)<\/td>/',
               $data, $matches, PREG_SET_ORDER);

foreach ($matches as $match) {
  $site_name = $match[2];
  $url = "http://www.mrrebates.com/{$match[1]}";
  $percent = $match[3];

  print "<a href='$url'>$site_name</a> ";
  print "<a href='$url'>$percent</a> <br/>";
}

That'll print out a list of links every time you refresh the page. I have no idea how referral codes work on that site, but I imagine it'll be pretty easy to tack it onto the $url variable.

One caveat here is that every time you refresh your page, it's going to have to load the other site first and parse it so it'll be slow. You could separate out the system("curl...") call into a separate file and only do that once an hour or so if you want to make it go faster. Good luck.


Parsing XHTML is best left to a DOM parser. However, this type of scrape operation is messy business anyway. I will propose another solution and let you piece it together.

View the source of your HTML and find out the beginning and end of your table. Looks like you want this:

<table border="0" width="95%" cellpadding="3" cellspacing="0" style="border: 1px dotted #808080;">

       <tr>

        <td bgcolor="#FFCC00"><b>Store Name</b></td>

        <td width="75" align="center" bgcolor="#FFCC00"><b>Coupons</b></td>

        <td width="75" align="right" bgcolor="#FFCC00"><b>Rebate</b></td>

       </tr>

And then look for the next occurrence of </table>.

Now, your content is in rows... look for <tr and </tr>.

I'll let you figure it out how to break it down from there.

Now, do actually all of this work... there are lots of functions that can help you. Start with strpos.


This is probably better done with javascript (or at least I have usually tackled problems like this on the client-side), particularly jQuery library.

You want to load the data on that page with something like

$.get("www.mrrebates.com/merchants/allmerchants.asp");

and parse the remaining data to get the info you need (this should be simple enough jQuery will do, tho there are fuller DOM parsers). I'm not sure what you're familiar with so far but it would probably be a lot to describe here. I see the % info is in td with class "r"

Do you have just one referral ID or one for each vender? that will obviously matter

0

精彩评论

暂无评论...
验证码 换一张
取 消