用python抓京東的產品數據

jopen 8年前發布 | 16K 次閱讀 網絡爬蟲

for i in range(11348876,11348999):#數字代表京東商品編號
    URL8=')

    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

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!