Call Taobao data can use the API provided by Taobao, if simply call Taobao merchandise picture name and other public information on their website, using PHP file_get_contents function can be implemented.
Ideas:
file_get_contents (URL) The function outputs the content of the Web page (the source code) as a string according to the URL such as http://www.baidu.com (an integer string) and then fits the Preg_match,preg_ These regular expression operations, such as replace, can be implemented to obtain information such as the URL-specific div,img. Of course, the first problem is Taobao in a single product page structure is fixed, such as 500 image of the IMG ID is j_imgbooth!
Specific implementation methods: (get 500 map, name, price, attributes and description of merchandise)
Copy Code code as follows:
$text =file_get_contents ("http://item.taobao.com/item.htm?id=2380347279"); Save the page content in the URL address into $text
A. Get 500 figure:
Copy Code code as follows:
Preg_match ('/]*id= "J_imgbooth" [^r]*rc=\ "([^"]*) \ "[^>]*>/', $text, $img);
Using a regular crawl img tag ID j_imgbooth img, $img [0] for the 500-figure img Tag, $img [1] is the picture address of the 500 image;
B. Get Name:
Copy Code code as follows:
Preg_match ('/<title> ([^<>]*) <\/title>/', $text, $title);
Because the text of the product name tag does not have a special class or ID is not good to crawl, grasp the contents of <title> tags, generally speaking, the content of the title is the name of the commodity (actually some access), $title [0] the entire title tag $title [1 ] The contents of the label;
$title =iconv (' GBK ', ' UTF-8 ', $title);
If your site is UTF8 code, then you need to do a bit of transcoding (Taobao is GBK code)
C. Access to prices:
Copy Code code as follows:
Preg_match ('/< ([a-z]+) [^i]*id=\ "J_strprice\" [^>]*> ([^<]*) <\/\\1>/is ', $text, $price);
Similarly get the label content of ID J_strprice $price[2], $price [0] is the entire label, $price [1] is the strong tag name;
$price =floatval ($price)//Put in the database estimate and turn the variable type
D. Get Properties:
What you get before this is done in a single label relative to just one regular, but if you want to get
Copy Code code as follows:
...
<div id= "xxx" >
...
<ul>
...
</ul>
<div>
<div>
</div>
</div>
</div>
...
Such a specific div has unknown n <> tags, it will be very difficult to get the specific div, search the Internet, the closest is only "/<" ([a-z]+) [^>]*> ([^<>]|) (? R) *<\/\\1>/"So use recursive crawl tag pairs, but he can't grab a specific tag, so want to easily crawl class=" attributes "div I can't do it. But the Taobao page has its particularity, is its each label structure basically is fixed ...<div>...</div> tag behind is not </div><div id= "description" > is </div ><div>, so we can use the workaround to get the property label content.
Copy Code code as follows:
Preg_match ('/< (div) [^c]*class=\ "attributes\" [^>]*>.*<\/\\1>/is ', $text, $text 0);
This will crawl <div start to the end of the entire page </div> tag, of course our property tag is in the front section of this.
$text 1=preg_replace ("/<\/div>[^<]*< (div) [^c]*id=\" description\ "[^>]*>.*<\/\\1>/is", ""], $text 0);
Match to </div ><div id= "description" > to last </div> then replace with "" (which is to delete the match), So if attributes's div is followed by description then we have achieved our goal.
$attributes =preg_replace ("/<\/div>[^<]*< (div) [^c]*class=\" Box j_tbox\ "[^>]*>.*<\/\\1>/ Is "," ", $text 1);
If the box J_tbox tag is followed by the attributes, then we need to use this step to remove the box J_tbox tag, of course, if the attributes div followed by description, This step will not be matched to anything that will not do anything.
E. Getting a description:
By using the above method you must feel that any label on the Taobao page can be easily obtained (as I thought before) but using this method to get the description of the content will be "description load", yes, this description is not in the source code, it is to open the page loaded into a large pile of JS, Do not know from the corner of Taobao loaded in.
Well, then we can also imitate it put some JS in. Not sure which is useful for load description? It's all right, it's all loaded in. Don't know what it takes to put those particular div up? Grab a source code, delete some of the Div step-by-step try, you will find "<div id=" Detail > </div>
Copy Code code as follows:
<div id= "description" >
<div id= "J_divitemdesc" > Description load </div>
</div>
These div are required to load the description, so here's the code:
Copy Code code as follows:
Preg_match_all ('/<script[^>]*>[^<]*<\/script>/is ', $text, $content);/page JS script
$content = $content [0];
$description = ' <div id= ' detail ' > </div>
<div id= "description" >
<div id= "J_divitemdesc" > Description load </div>
</div> ';
foreach ($content as & $v) {$description. =iconv (' GBK ', ' UTF-8 ', $v);
Put this $description into the page, the description will be automatically loaded in, of course, multiple products described on the same page will only have a description will be loaded.