fapapfap Posted December 28, 2011 Share Posted December 28, 2011 So I would like to scrape a page to make a list of URLs. The source has lots of URLs in the following format: <a href="http://www.site.com/page.php?action=p&id=99">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=97">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=98">TEXT</a> and carries on in exactly the same format. Can someone help me with the code I need to scrape the page and extract just the links into a list using regex? Quote Link to comment Share on other sites More sharing options...
scootstah Posted December 28, 2011 Share Posted December 28, 2011 Seems to work: <?php $pattern = '~<a href="http:\/\/www\.site\.com\/page\.php\?action=p\&id=[0-9]+">[a-zA-Z]+<\/a>~i'; $text = 'So I would like to scrape a page to make a list of URLs. The source has lots of URLs in the following format: <a href="http://www.site.com/page.php?action=p&id=99">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=97">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=98">TEXT</a> and carries on in exactly the same format. Can someone help me with the code I need to scrape the page and extract just the links into a list using regex?'; if (preg_match_all($pattern, $text, $matches)) { echo '<pre>' . print_r($matches) . '</pre>'; } Quote Link to comment Share on other sites More sharing options...
fapapfap Posted December 28, 2011 Author Share Posted December 28, 2011 Seems to work: <?php $pattern = '~<a href="http:\/\/www\.site\.com\/page\.php\?action=p\&id=[0-9]+">[a-zA-Z]+<\/a>~i'; $text = 'So I would like to scrape a page to make a list of URLs. The source has lots of URLs in the following format: <a href="http://www.site.com/page.php?action=p&id=99">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=97">TEXT</a>, <a href="http://www.site.com/page.php?action=p&id=98">TEXT</a> and carries on in exactly the same format. Can someone help me with the code I need to scrape the page and extract just the links into a list using regex?'; if (preg_match_all($pattern, $text, $matches)) { echo '<pre>' . print_r($matches) . '</pre>'; } Thank you so much, perfect! Quote Link to comment Share on other sites More sharing options...
fapapfap Posted December 28, 2011 Author Share Posted December 28, 2011 Another question please: Some of the data will be fortmatted in the following form, how to deal with this in the context of the answer above? <a href="http://www.site.com/page.php?action=p&uid=448"><span style="color:#0066FF;">TEXT</span></a> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.