Web Scraping with PHP Matthew Turland September 16, 2008
Everyone acquainted? <ul><li>Lead Programmer for surgiSYS, LLC </li></ul><ul><li>PHP Community member </li></ul><ul><li>Blog: http://ishouldbecoding.com </li></ul>
What is Web Scraping? 2 Stage Process Stage 1 : Retrieval GET /some/resource ... HTTP/1.1 200 OK ... Resource with data you want Stage 2 : Analysis Raw resource Usable data
How is it different from... Data Mining Focus in data mining Focus in web scraping Consuming Web Services Web service data formats Web scraping data formats
Potential Applications What Data source When Web service is unavailable or data access is one-time only. Crawlers and indexers Remote data search offers no capabilities for search or data source integration. Integration testing Applications must be tested by simulating client behavior and ensuring responses are consistent with requirements.
Know enough HTTP to... Use one like this: To do this:
Know enough HTTP to... PEAR::HTTP_Client pecl_http Zend_Http_Client Learn to use and troubleshoot one like this: Or roll your own! cURL Filesystem + Streams
<ul><li>GET /wiki/Main_Page HTTP/1.1 </li></ul><ul><li>Host: en.wikipedia.org </li></ul>Let's GET Started method or operation URI address for the desired resource protocol version in use by the client header name header value request line header more headers follow...
Warning about GET In principle: "Let's do this by the book." GET In reality: "' Safe operation '? Whatever." GET
URI vs URL 1. Uniquely identifies a resource 2. Indicates how to locate a resource 3. Does both and is thus human-usable. URI URL More info in RFC 3986 Sections 1.1.3 and 1.2.2
Query Strings http://en.wikipedia.org/w/index.php? title=Query_string&action=edit URL Query String Question mark to separate the resource address and query string Equal signs to separate parameter names and respective values Ampersands to separate parameter name-value pairs. Parameter Value
URL Encoding Parameter Value first second this is a field was it clear enough (already)? Query String first=this+is+a+field&second=was+it+clear+%28already%29%3F Also called percent encoding . urlencode and urlencode : Handy PHP URL functions $_SERVER ['QUERY_STRING'] / http_build_query ( $_GET ) More info on URL encoding in RFC 3986 Section 2.1
POST Requests Most Common HTTP Operations 1. GET 2. POST ... /w/index.php POST /new/resource -or- /updated/resource GET /some/resource HTTP/1.1 Header: Value ... POST /some/resource HTTP/1.1 Header: Value request body none
POST Request Example <ul><li>POST /w/index.php?title=Wikipedia:Sandbox HTTP/1.1 </li></ul><ul><li>Content-Type: application/x-www-form-urlencoded </li></ul><ul><li>wpStarttime=20080719022313&wpEdittime=20080719022100... </li></ul>Blank line separates request headers and body Content type for data submitted via HTML form (multipart/form-data for file uploads ) Request body ... look familiar? Note : Most browsers have a query string length limit. Lowest known common denominator: IE7 – strlen(entire URL) <= 2,048 bytes. This limit is not standardized and only applies to query strings, not request bodies.
HEAD Request <ul><li>HEAD /wiki/Main_Page HTTP/1.1 Host: en.wikipedia.org </li></ul>Same as GET with two exceptions: 1 <ul><li>HTTP/1.1 200 OK Header: Value </li></ul>2 No response body HEAD vs GET Sometimes headers are all you want ?
Responses HTTP/1.0 200 OK Server: Apache X-Powered-By: PHP/5.2.5 ... [body] Lowest protocol version required to process the response Response status code Response status description Status line Same header format as requests, but different headers are used (see RFC 2616 Section 14 )
Response Status Codes <ul><li>1xx Informational Request received, continuing process. </li></ul><ul><li>2xx Success Request received, understood, and accepted. </li></ul><ul><li>3xx Redirection Client must take additional action to complete the request. </li></ul><ul><li>4xx Client Error Request is malformed or could not be fulfilled. </li></ul><ul><li>5xx Server Error Request was valid, but the server failed to process it. </li></ul>See RFC 2616 Section 10 for more info.
Headers Set-Cookie Cookie Location Watch out for infinite loops! Last-Modified If-Modified-Since 304 Not Modified ETag If-None-Match OR See RFC 2109 or RFC 2965 for more info.
More Headers WWW-Authenticate Authorization User-Agent 200 OK / 403 Forbidden See RFC 2617 for more info. User-Agent: Some servers perform user agent sniffing Some clients perform user agent spoofing
Best Practices <ul><li>If responses are unlikely to change often, cache them locally and check for updates with HEAD requests. </li></ul><ul><li>If possible, measure target server response time and vary when requests are sent based on peak usage times. </li></ul><ul><li>Replicate normal user behavior (particularly headers) as closely as possible. If the application is flexible, vary behavior where it makes sense for performance. </li></ul>
Streams Resources <ul><li>Language Reference > Context options and parameters </li></ul><ul><ul><li>HTTP context options </li></ul></ul><ul><ul><li>Context parameters </li></ul></ul><ul><li>Appendices > List of Supported Protocols/Wrappers </li></ul><ul><ul><li>HTTP and HTTPS </li></ul></ul><ul><li>php|architect's Definitive Guide to PHP Streams (ETA late 2008 / early 2009) </li></ul>
cURL Examples Fatal error: Allowed memory size of n00b bytes exhausted (tried to allocate 1337 bytes) in /this/slide.php on line 1 See PHP Manual , Context Options , or my php|architect article for more info. Just kidding. Really, the equivalent cURL code for the previous examples is so verbose that it won't fit on one slide and I don't think it's deserving of multiple slides.
HTTP Resources <ul><li>RFC 2616 HyperText Transfer Protocol </li></ul><ul><li>RFC 3986 Uniform Resource Identifiers </li></ul><ul><li>"HTTP: The Definitive Guide" (ISBN 1565925092) </li></ul><ul><li>"HTTP Pocket Reference: HyperText Transfer Protocol" (ISBN 1565928628) </li></ul><ul><li>"HTTP Developer's Handbook" (ISBN 0672324547) by Chris Shiflett </li></ul><ul><li>Ben Ramsey's blog series on HTTP </li></ul>
Analysis Raw resource Usable data DOM XMLReader SimpleXML XSL tidy PCRE String functions JSON ctype XML Parser
Cleanup <ul><li>tidy is good for correcting markup malformations. * </li></ul><ul><li>String functions and PCRE can be used for manual cleanup prior to using a parsing extension. </li></ul><ul><li>DOM is generally forgiving when parsing malformed markup. It generates warnings that can be suppressed. </li></ul><ul><li>Save a static copy of your target, use a validator on the input (ex: W3C Markup Validator ), fix validation errors manually, and write code to automatically apply fixes. </li></ul>
Validation <ul><li>Make as few assumptions (and as many assertions) about the target as possible. </li></ul><ul><li>Validation provides additional sanity checks for your application. </li></ul><ul><li>PCRE can be used to form pattern-based assertions about extracted data. </li></ul><ul><li>ctype can be used to form primitive type-based assertions. </li></ul>
Transformation <ul><li>XSL can be used to extract data from an XML-compatible document and retrofit it to a format defined by an XSL template. </li></ul><ul><li>To my knowledge, this capability is unfortunately unique to XML-compatible data. </li></ul><ul><li>Use components like template engines to separate formatting of data from retrieval/analysis logic. </li></ul>
Abstraction <ul><li>Remain in keeping with the DRY principle. </li></ul><ul><li>Develop components that can be reused across projects. Ex: DomQuery , Zend_Dom . </li></ul><ul><li>Make an effort to minimize application-specific logic. This applies to both retrieval and analysis. </li></ul>
Assertions <ul><li>Apply to long-term real-time web scraping applications. </li></ul><ul><li>Affirm conditions of behavior and output of the target application. </li></ul><ul><li>Use in the application during runtime to avoid Bad Things (tm) happening when the target application changes. </li></ul><ul><li>Include in unit tests of the application. You are using unit tests, right? </li></ul>
Testing <ul><li>Write tests on target application output stored in local files that can be run sans internet during development. </li></ul><ul><li>If possible/feasible/appropriate, write "live tests" that actively test using assertions on the target application. </li></ul><ul><li>Run live tests when the target appears to have changed (because your web scraping application breaks). </li></ul>
Questions? <ul><li>No heckling... OK, maybe just a little. </li></ul><ul><li>I will hang around afterward if you have questions, points for discussion, or just want to say hi. It's cool, I don't bite or have cooties or anything. I have business cards too. </li></ul><ul><li>I generally blog about my experiences with web scraping and PHP at http://ishouldbecoding.com. </shameless_plug> </li></ul><ul><li>Thanks for coming! </li></ul>