2019独角兽企业重金招聘Python工程师标准>>>
for i in range(11348876,11348999):#数字代表京东商品编号URL8='http://item.jd.com/%s.html'%(i)page=urllib.request.urlopen(URL8).read()#page=urllib.urlopen(URL).read()pagenew = page.decode("GBK")idx=pagenew.find('product:')if(idx>=0):idx+= 8res = 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']))
记录几个知识点:
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(
"'"
,
"\""
);
给属性添加单引号;
把所有的单引号替换成双引号;