| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
8年前发布

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

记录几个知识点:

      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

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1452173395011.html
网络爬虫