Python 将 html 文档 表格 转 为 JSON 格式

Python 将 html 文档 表格 转 为 JSON 格式
测试示例:

import requests
from lxml import etree
import pandas as pd

url = "http://xxx"

header = {
    "Cookie": "xxx"
}
res = requests.get(url, headers=header)
doc = etree.HTML(res.text)

columns = doc.xpath('/html/body/div/div[2]/div/div[2]/table/thead/tr/th/text()')
data = doc.xpath('/html/body/div/div[2]/div/div[2]/table/tbody/tr')
# print(columns)
# print(data)
df = pd.DataFrame(columns=columns)


def trans_value(pe, path: str) -> str:
    if type(pe.xpath(path)) is list and len(pe.xpath(path)) == 1:
        return str(pe.xpath(path)[0])
    else:
        return ''


for row in data:
    td = row.xpath('./td')
    # print(td)
    temp = []
    for td_row in td:
        # print(td_row.xpath('./*'))
        a = td_row.xpath('./a')
        if a != [] and a[0].xpath('./text()') == ['修改']:
            # temp.append(transValue(a[0], './@href'))
            temp.append('')
            continue
        if a != [] and a[0].xpath('./text()') == ['播放']:
            audio_tl = trans_value(a[0], './@onclick').replace("'", '').split(',')
            if len(audio_tl) == 3:
                temp.append(audio_tl[1])
            else:
                temp.append('')
            continue
        temp.append(trans_value(td_row, './text()'))
    # print(temp)
    df.loc[len(df)] = temp
# df.to_excel('out.xlsx', index=False)
with open('out.json', 'w', encoding='utf8') as f:
    f.write(df.to_json(orient='records', force_ascii=False))

print('处理完成')

你可能感兴趣的:(Python,后端,数据分析,python,html,json)