日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Beautifulsoup 网页表格的解析

發(fā)布時(shí)間:2024/9/27 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Beautifulsoup 网页表格的解析 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Beautifulsoup 網(wǎng)頁表格的解析

眾所周知網(wǎng)頁的表格標(biāo)簽里的文本需要按照表格的大小逐行逐列解析才能得到結(jié)構(gòu)化的數(shù)據(jù)否則得到的內(nèi)容則會(huì)錯(cuò)位。

原始網(wǎng)頁:

代碼示例:

if len(potentialtable) > 0:##print(str(tableContent[1]).replace('\xa0',' '))for tab in potentialtable:x = (len(tab.findAll('tr')))startidx = tab.sourcelinestrtemp = ""relend = 0realtabsize = 0print(tab.name + "\t where is my way")if (tab.name == 'table'):realtabsize = realtabsize + 1for row in tab.findAll('tr')[0:x]:y = (len(row.findAll('td')))if y == 0 or y == 1:endidx = row.sourcelineelse:endidx = row.findAll('td')[-1].sourcelineprint('endidx\t'+str(endidx))lastrow = row.findAll('td')[-1]lenchild=len(lastrow.find_all(lambda x: x.name != '', recursive=False))if lenchild >0:for i in lastrow.descendants:typename = str(type(i)).replace("<class 'bs4.element.", '').replace("'>", '')if (typename == 'NavigableString'):continueelse:relend = i.sourcelineprint('relend\t'+str(relend))else:relend =0for td in row.findAll('td')[0:y]:strtemp = strtemp + td.get_text().replace('\n', '') + "\t"strtemp = strtemp.rstrip('\t') + "\01" # + "^"relend = endidx if (relend == 0) else relendnewpd = pd.DataFrame({"bindid": bidid + '_' + str(catalog)[::-1][0], "start": startidx, "end": relend,"cont": strtemp.replace('\xa0', ' '), 'catalog': 4, 'realbindid': bidid,'bindkind': str(catalog)[::-1][0]}, index=["0"])df_htmlreplace = df_htmlreplace.append(newpd, ignore_index=True, sort=True)

執(zhí)行結(jié)果(\t分割):

合同編號(hào): BZGC2019042-01 合同名稱: 消防改造工程第1標(biāo)段 標(biāo)段(包)名稱: 消防改造工程第1標(biāo)段 標(biāo)段(包)編號(hào): BZGC2019042-01 合同甲方名稱: 人民醫(yī)院 合同乙方名稱: 金沙灘科技有限公司 合同金額: 852633.7 金額單位: 元 合同期限(天): 100 合同簽署日期: 2019年08月19日 合同完成日期: 2020年04月30日 質(zhì)量要求: 工程質(zhì)量符合達(dá)到國家現(xiàn)行有關(guān)施工質(zhì)量驗(yàn)收規(guī)范標(biāo)準(zhǔn)

總結(jié)

以上是生活随笔為你收集整理的Beautifulsoup 网页表格的解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。