最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python基于pandas實(shí)現(xiàn)json格式轉(zhuǎn)換成dataframe的方法

 更新時(shí)間:2018年06月22日 09:28:36   作者:zn505119020  
這篇文章主要介紹了Python基于pandas實(shí)現(xiàn)json格式轉(zhuǎn)換成dataframe的方法,結(jié)合實(shí)例形式分析了Python使用pandas模塊操作json數(shù)據(jù)轉(zhuǎn)換成dataframe的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Python基于pandas實(shí)現(xiàn)json格式轉(zhuǎn)換成dataframe的方法。分享給大家供大家參考,具體如下:

# -*- coding:utf-8 -*-
#!python3
import re
import json
from bs4 import BeautifulSoup
import pandas as pd
import requests
import os
from pandas.io.json import json_normalize
class image_structs():
  def __init__(self):
    self.picture_url = {
      "image_id": '',
      "picture_url": ''
    }
class data_structs():
  def __init__(self):
    # columns=['title', 'item_url', 'id','picture_url','std_desc','description','information','fitment'])
    self.info={
      "title":'',
      "item_url":'',
      "id":0,
      "picture_url":[],
      "std_desc":'',
      "description":'',
      "information":'',
      "fitment":''
    }
# "https://waldoch.com/store/catalogsearch/result/index/?cat=0&limit=200&p=1&q=nerf+bar"
# https://waldoch.com/store/new-oem-ford-f-150-f150-5-running-boards-nerf-bar-crew-cab-2015-w-brackets-fl34-16451-ge5fm6.html
def get_item_list(outfile):
  result = []
  for i in range(6):
    print(i)
    i = str(i+1)
    url = "https://waldoch.com/store/catalogsearch/result/index/?cat=0&limit=200&p="+i+"&q=nerf+bar"
    web = requests.get(url)
    soup = BeautifulSoup(web.text,"html.parser")
    alink = soup.find_all("a",class_="product-image")
    for a in alink:
      title = a["title"]
      item_url = a["href"]
      result.append([title,item_url])
  df = pd.DataFrame(result,columns=["title","item_url"])
  df = df.drop_duplicates()
  df["id"] =df.index
  df.to_excel(outfile,index=False)
def get_item_info(file,outfile):
  DEFAULT_FALSE = ""
  df = pd.read_excel(file)
  for i in df.index:
    id = df.loc[i,"id"]
    if os.path.exists(str(int(id))+".xlsx"):
      continue
    item_url = df.loc[i,"item_url"]
    url = item_url
    web = requests.get(url)
    soup = BeautifulSoup(web.text, "html.parser")
    # 圖片
    imglink = soup.find_all("img", class_=re.compile("^gallery-image"))
    data = data_structs()
    data.info["title"] = df.loc[i,"title"]
    data.info["id"] = id
    data.info["item_url"] = item_url
    for a in imglink:
      image = image_structs()
      image.picture_url["image_id"] = a["id"]
      image.picture_url["picture_url"]=a["src"]
      print(image.picture_url)
      data.info["picture_url"].append(image.picture_url)
    print(data.info)
    # std_desc
    std_desc = soup.find("div", itemprop="description")
    try:
      strings_desc = []
      for ii in std_desc.stripped_strings:
        strings_desc.append(ii)
      strings_desc = "\n".join(strings_desc)
    except:
      strings_desc=DEFAULT_FALSE
    # description
    try:
      desc = soup.find('h2', text="Description")
      desc = desc.find_next()
    except:
      desc=DEFAULT_FALSE
    description=desc
    # information
    try:
      information = soup.find("h2", text='Information')
      desc = information
      desc = desc.find_next()
    except:
      desc=DEFAULT_FALSE
    information = desc
    # fitment
    try:
      fitment = soup.find('h2', text='Fitment')
      desc = fitment
      desc = desc.find_next()
    except:
      desc=DEFAULT_FALSE
    fitment=desc
    data.info["std_desc"] = strings_desc
    data.info["description"] = str(description)
    data.info["information"] = str(information)
    data.info["fitment"] = str(fitment)
    print(data.info.keys())
    singledf = json_normalize(data.info,"picture_url",['title', 'item_url', 'id', 'std_desc', 'description', 'information', 'fitment'])
    singledf.to_excel("test.xlsx",index=False)
    exit()
    # print(df.ix[i])
  df.to_excel(outfile,index=False)
# get_item_list("item_urls.xlsx")
get_item_info("item_urls.xlsx","item_urls_info.xlsx")

這里涉及到的幾個(gè)Python模塊都可以使用pip install命令進(jìn)行安裝,如:

pip install BeautifulSoup4

pip install xlrd

pip install openpyxl

PS:這里再為大家推薦幾款比較實(shí)用的json在線(xiàn)工具供大家參考使用:

在線(xiàn)JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線(xiàn)格式化工具:
http://tools.jb51.net/code/jsonformat

在線(xiàn)XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線(xiàn)格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線(xiàn)json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python操作json技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python利用公共鍵如何對(duì)字典列表進(jìn)行排序詳解

    Python利用公共鍵如何對(duì)字典列表進(jìn)行排序詳解

    這篇文章主要給大家介紹了關(guān)于Python利用公共鍵如何對(duì)字典列表進(jìn)行排序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • python爬蟲(chóng)(入門(mén)教程、視頻教程)

    python爬蟲(chóng)(入門(mén)教程、視頻教程)

    本文是一篇關(guān)于python爬蟲(chóng)的知識(shí)學(xué)習(xí)教程,并且給大家解答了學(xué)習(xí)了python爬蟲(chóng)怎么掙錢(qián)這個(gè)很多程序員疑惑的問(wèn)題,如果你對(duì)這方面的知識(shí)也感興趣,一起深入學(xué)習(xí)一下吧。
    2018-01-01
  • Python3.5面向?qū)ο缶幊虉D文與實(shí)例詳解

    Python3.5面向?qū)ο缶幊虉D文與實(shí)例詳解

    這篇文章主要介紹了Python3.5面向?qū)ο缶幊?結(jié)合圖文與實(shí)例形式詳細(xì)分析了Python面向?qū)ο缶幊滔嚓P(guān)的概念、類(lèi)定義、實(shí)例化、實(shí)例變量、類(lèi)變量、析構(gòu)函數(shù)等相關(guān)原理及使用技巧,需要的朋友可以參考下
    2019-04-04
  • Python實(shí)現(xiàn)批量修改圖片大小

    Python實(shí)現(xiàn)批量修改圖片大小

    在日常生活中經(jīng)常有需要調(diào)整圖片大小的需求,一張兩張的還好,但是如果需要幾十張幾百?gòu)埳踔翈浊?,就可以考慮使用Python腳本來(lái)執(zhí)行了,下面小編就來(lái)和大家分享幾個(gè)Python可以批量修改圖片大小的腳本吧
    2023-07-07
  • Python數(shù)組并集交集補(bǔ)集代碼實(shí)例

    Python數(shù)組并集交集補(bǔ)集代碼實(shí)例

    這篇文章主要介紹了Python數(shù)組并集交集補(bǔ)集代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Django如何簡(jiǎn)單快速實(shí)現(xiàn)PUT、DELETE方法

    Django如何簡(jiǎn)單快速實(shí)現(xiàn)PUT、DELETE方法

    這篇文章主要介紹了Django如何簡(jiǎn)單快速實(shí)現(xiàn)PUT、DELETE方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python多進(jìn)程日志以及分布式日志的實(shí)現(xiàn)方式

    python多進(jìn)程日志以及分布式日志的實(shí)現(xiàn)方式

    這篇文章主要介紹了python多進(jìn)程日志以及分布式日志的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • 十道Python面試最常問(wèn)到的問(wèn)題

    十道Python面試最常問(wèn)到的問(wèn)題

    這篇文章主要介紹了十道Python面試最常問(wèn)到的問(wèn)題,都是基礎(chǔ)的面試題,包括函數(shù),修飾器,數(shù)據(jù)結(jié)構(gòu)等,需要的朋友可以參考下
    2023-04-04
  • Celery+django+redis異步執(zhí)行任務(wù)的實(shí)現(xiàn)示例

    Celery+django+redis異步執(zhí)行任務(wù)的實(shí)現(xiàn)示例

    本文主要介紹了Celery+django+redis異步執(zhí)行任務(wù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • 基于Python實(shí)現(xiàn)簡(jiǎn)易的動(dòng)漫圖片轉(zhuǎn)換器

    基于Python實(shí)現(xiàn)簡(jiǎn)易的動(dòng)漫圖片轉(zhuǎn)換器

    本文旨在利用Python語(yǔ)言制作一個(gè)將普通照片轉(zhuǎn)換成動(dòng)漫圖片的小工具,其中使用opencv的非標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)對(duì)圖片完成轉(zhuǎn)換,感興趣的可以嘗試一下
    2022-08-08

最新評(píng)論

内江市| 吉木萨尔县| 德州市| 隆回县| 乌鲁木齐市| 新蔡县| 湘潭县| 连平县| 理塘县| 梅州市| 崇州市| 望城县| 霍山县| 鄄城县| 静安区| 翼城县| 彩票| 同仁县| 同心县| 安阳县| 平舆县| 滦南县| 营山县| 汪清县| 隆林| 潢川县| 阿拉善左旗| 双鸭山市| 伊通| 齐河县| 腾冲县| 武川县| 太康县| 云龙县| 广饶县| 鹿泉市| 南昌县| 米脂县| 苏尼特右旗| 洞口县| 长寿区|