)
page=urllib.request.urlopen(URL8).read()
#page=urllib.urlopen(URL).read()
pagenew = page.decode("GBK")
idx=pagenew.find('product:')
if(idx>=0):
idx+= 8
res = pagenew[idx:pagenew.find('};')]
res=res.strip()
addedSingleQuoteJsonStr = re.sub(r"(,?)(\w+?)\s*?:", r"\1'\2':", res);
doubleQuotedJsonStr = addedSingleQuoteJsonStr.replace("'", "\"");
doubleQuotedJsonStr = doubleQuotedJsonStr.replace("\"http\"", "http");
print(doubleQuotedJsonStr)
text=JSONDecoder().decode(doubleQuotedJsonStr)#用json讀取
print(text)
print("%s,%s,%s,%s,%s"%(text['skuid'],text['wMaprice'],text['name'],text['href'],text['jqimg']))</pre>
記錄幾個知識點:
1:
ValueError: Expecting property name: line 1 column 1 (char 1)
類型的錯誤,就是由于JSON中,標準語法中,不支持單引號,
屬性或者屬性值,都必須是雙引號括起來的。
所以,可以用類似于:
addedSingleQuoteJsonStr
=
re.sub(r
"(,?)(\w+?)\s*?:"
, r
"\1'\2':"
, orginalJsonStr);
doubleQuotedJsonStr
=
addedSingleQuoteJsonStr.replace(
"'"
,
"\""
);
來自: http://my.oschina.net/u/1462124/blog/599053