• 注册
当前位置:1313e > python >正文

用python抓京东的产品数据

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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( "'" , "\"" );
  • 给属性添加单引号;

  • 把所有的单引号替换成双引号;


转载于:https://my.oschina.net/u/1462124/blog/599053

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录