Using curl and a user agent string for web scraping pt 2; Now with PHP

First off, Happy Thanksgiving to everyone, US readers especially. I know I should have written this sooner, but the holiday kept me pretty busy this year. And I hope you can accept my late well wishes.

And after you've had your fill of turkey, stuffing, and some pumpkin pie maybe you got the chance to play around with what I posted last week. And now lets build from that. Let's change the situation around a little bit; and say that a command line utility isn't your thing for this project. And you would like to use PHP.

First off, you'll want to make sure that your web server has curl compiled in. And the easiest way is to use phpinfo() to find it, and you'll want to look for this:

If you see something similar to what I have above, your good. Otherwise you'll either need to recompile your php to include libcurl, or complain to your hosting provider. Afterwards we can accomplish what we want with a couple lines of code:

= array(
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20091030 Firefox/3.5.4",

$ch curl_init($url);
$content curl_exec($ch);

And now in your PHP code, you'll have a variable called $content that will contain all of the html code from the website. And how you wanna parse information outside of that is up to you. Of course if anyone has questions I will be happy to help out as best as I can.

Using curl and a user agent string for web scraping

According to Wikipedia web scraping is "is a computer software technique of extracting information from websites." So let's say you wanted to write a script to grab the number of trophies from Sony's PS3 system. Web scraping might be a technique you might want to consider. First move would be to use a web browser to get the URL to eventually scrape:

"" (yes, that is my ps3 user id and yes those are my stats)

Next step one would probably use a text browser to facilitate the web scraping, like wget. So next step would be to put these two together and see what happens:

  1. wget

But what is this:

  1. wget <a href="
  2. --2009-11-17" title="
  3. --2009-11-17">
  4. --2...</a> 19:52:56-- <a href="
  5. Resolving" title="
  6. Resolving">
  7. Res...</a>
  8. Connecting to||:80... connected.
  9. HTTP request sent, awaiting response... 403 Forbidden
  10. 2009-11-17 19:52:56 ERROR 403: Forbidden.

Let's see if this is what happens when we use curl (another website grabber):

Now the response is even different than when using wget, but the most important part is in the middle of the response ( shortened for readability):

  1. <h2 align="center">Forbidden</h2>
  2. <h4 align="center">Access is forbidden to this URL</h4>

So, now we see 3 inconsistencies here. With a "normal" web browser, things work fine. Wget is down right rejected, and curl is reject but a little more politely. At this point it looks like our web scraping project would seem lost, but I don't give up that quickly. One thing these three things have in common is a user agent. So if we can tell the website we are a different user agent than what we really are, things should work. Luckily enough for curl has this ability. And after a little research we can now try new command to see if it works:

curl -A 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv: Gecko/20091030 Gentoo Firefox/3.5.4'

And VIOLA, we now get access to the site as if we were using firefox. (html code not included for readability. If you wanna see the code you can run it yourself.)

This is a good idea to keep in your mental toolbox, you never know when you might need a trick like this in the future.

Update (4/18/2010): A nice guy named John over at emailed me and told me that the link above no longer works. I tested it myself and John is correct. It turns out that Sony changed things up a little bit and now the trophies page is all done in javascript. Being that javascript isn't html the code above will not work.

If you made it this far down into the article, hopefully you liked it enough to share it with your friends. Thanks if you do, I appreciate it.

Bookmark and Share

Syndicate content