用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+= 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']))

记录几个知识点:html

      1:python

ValueError: Expecting property name: line 1 column 1 (char 1)json

类型的错误,就是因为JSON中,标准语法中,不支持单引号,ui

属性或者属性值,都必须是双引号括起来的。url

因此,能够用相似于:spa

addedSingleQuoteJsonStr = re.sub(r "(,?)(\w+?)\s*?:" , r "\1'\2':" , orginalJsonStr);
doubleQuotedJsonStr = addedSingleQuoteJsonStr.replace( "'" , "\"" );
  • 给属性添加单引号;code

  • 把全部的单引号替换成双引号;orm

相关文章
相关标签/搜索