對京東某一商品信息頁面的HTML代碼進行分析。可以發現它的圖書產品信息頁面都含有這樣一段代碼(不一樣類的商品頁面有些不一樣):javascript
window.pageConfig={compatible:true,searchType: 1,product:{"skuid":"11408255","name":"\u4f17\u795e\u7684\u536b\u661f\uff1a\u4e2d\u56fd\u7981\u533a","skuidkey":"0337880E5D0F419E033111D988169617","href":"http:\/\/item.jd.com\/11408255.html","src":"g14\/M01\/13\/0F\/rBEhVVMPSugIAAAAAAViPV7x4XAAAJS4gLble8ABWJV204.jpg","jqimg":"http:\/\/img11.360buyimg.com\/n0\/g14\/M01\/13\/0F\/rBEhVVMPSugIAAAAAAViPV7x4XAAAJS4gLble8ABWJV204.jpg","wMaprice":"32.00","wMeprice":"23.40","cat":[1713,9340,9346],"brand":"","tips":false,"type":1,"n":false,"g":false}};
import json import re import urllib for i in range(11348876,11348999):#數字表明京東商品編號 URL='http://item.jd.com/%s.html'%(i) page=urllib.urlopen(URL).read() idx=page.find('product:') if(idx>=0): idx+=8 res=re.search(r'{.+?}',page[idx:]) #用正則表達式匹配 text=json.loads(res.group()) #用json讀取 print("%s,%s,%s,%s,%s"%(text['skuid'],text['wMaprice'],text['name'],text['href'],text['jqimg']))