Extract a specific part from a html document , php cURL , php, preg_match
I'm trying to extract some information from a webpage using php cURL+preg_match or any other function but for some reasons it doesn't work at all . For example from this page, I want to extract the title which is "4 bed house to rent, Caroline Place, Bayswater, W2", the price which is "2,300" and the description which starts at "This fantastic..." and ends at "(Circle and District Lines). ". 开发者_高级运维 I tried to use php cURL + dom but I'm getting a lot of errors like this "htmlParseEntityRef: expecting ';' in Entity, line: 243" and no result displayed
Also I tried to use preg_match or preg_match_all but doesn't work either .
A very basic example would be highly appreciated !
A very basic example would be highly appreciated
To answer the regex part:
preg_match('!<title>(.*)</title>!s', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
4 bedroom
house
to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)</title>
<meta name="keywords" content="Houses" />', $matches);
print_r($matches);
/* output:
Array
(
[0] => <title>
4 bedroom
house
to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)</title>
[1] =>
4 bedroom
house
to rent in Caroline Place, Bayswater, W2 through Foxtons (Property to rent)
)
*/
The s
at the end of the regex puts the parser into something (inaptly) called single-line mode
.
You could try whether the Simple HTML DOM parser is more fault tolerant.
And take note of the Terms & Conditions of the site you are scraping.
I cannot give a high enough recommendation for HTMLsql:
http://www.jonasjohn.de/lab/htmlsql.htm
This puppy has saved me many times in too many ways to count.
after fetching data through curl, the resultant has many new lines and spaces in it. So, perform some clean html script in order to remove this new lines and spaces. Finally, have a happy preg_match
精彩评论