baidu文库爪巴虫——xls
生活随笔
收集整理的這篇文章主要介紹了
baidu文库爪巴虫——xls
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
用開(kāi)發(fā)者工具從下載文件的 response 中找文檔內(nèi)容,很快就找到了
分析請(qǐng)求地址及參數(shù)
然后回到文檔源代碼頁(yè)面,發(fā)現(xiàn)這個(gè)請(qǐng)求地址已經(jīng)包含在里面了,只需要把它提取出來(lái)即可
表格和word 文檔還是不一樣的,表格還要考慮文本的橫向距離,區(qū)分不同的列
然而并沒(méi)有什么好辦法確定文本到底在哪一列,是能確定個(gè)大概順序
import requests import re import pandas as pd import matplotlib.pyplot as plt %matplotlib inlineurl = 'https://wenku.baidu.com/view/3f2024bc852458fb760b56e3.html?fr=search'html = requests.get(url).textjs = re.findall(r'"pageLoadUrl.*?(https.*?0.json.*?)\\', html)hjs = [item.replace('\\\\/', '/') for item in js]for i in hjs:try:new_html = requests.get(i).texttxtlist=re.findall('"c":"(.*?)".*?"x":(.*?),"y":(.*?),',new_html)c, x, y = list(zip(*txtlist))x, y = [float(i) for i in x],[int(float(i)) for i in y]# -----畫(huà)圖看看分布-------------fig, ax = plt.subplots(1,1,figsize=(4,6))plt.plot(x,y,'.')ax.invert_yaxis()plt.show()# ------------------df = pd.DataFrame({'c':c, 'x':x, 'y':y})df.sort_values(by=['y','x'], inplace=True)row=0result = ''for _, item in df.iterrows():if row == item[2]:result += ','else:row = item[2]result += '\n'result+=item['c'].encode('utf-8').decode('unicode_escape','ignore')print(result)except Exception as e:print(e)看看第一頁(yè),這是根據(jù) x,y 坐標(biāo)返回的散點(diǎn)圖
我發(fā)現(xiàn) Di03PrgCode3 這個(gè)文本輸出的時(shí)候另起了一行???原來(lái)是因?yàn)樗?y 坐標(biāo)比同一行的文本稍大了一點(diǎn)
把 y 坐標(biāo)輸出來(lái)看,:
現(xiàn)在能想到的解決思路就是利用聚類(lèi)算法,把近似出于同一行但是y坐標(biāo)稍有不同的文本劃分到同一類(lèi)中!
使用 DBSCAN 完美解決!!!
為什么用DBSCAN呢?因?yàn)椴挥迷O(shè)置聚類(lèi)數(shù)目,只需要設(shè)定類(lèi)內(nèi)相鄰樣本的最大間距,和我的場(chǎng)景完美符合!!
import requests import re import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import DBSCAN %matplotlib inlineurl = 'https://wenku.baidu.com/view/3f2024bc852458fb760b56e3.html?fr=search'html = requests.get(url).textjs = re.findall(r'"pageLoadUrl.*?(https.*?0.json.*?)\\', html)hjs = [item.replace('\\\\/', '/') for item in js]for i in hjs:try:new_html = requests.get(i).texttxtlist=re.findall('"c":"(.*?)".*?"x":(.*?),"y":(.*?),',new_html)c, x, y = list(zip(*txtlist))x, y = [float(i) for i in x],[int(float(i)) for i in y]# -----畫(huà)圖看看分布-------------fig, ax = plt.subplots(1,1,figsize=(4,6))plt.plot(x,y,'.')ax.invert_yaxis()plt.show()# ------DBSCAN對(duì)y坐標(biāo)聚類(lèi)----------data = np.ones((len(y),2))data[:,1] = ymax_error = 2db = DBSCAN(eps=max_error, min_samples=1).fit(data)labels = db.labels_# ------根據(jù)類(lèi)別確定行號(hào)original_seq = [i for i in range(len(y))]pairs = np.array(sorted(list(zip(y,original_seq,labels)))) # (列, 原始順序,類(lèi)別)row = [0 for _ in range(len(y))]for i in range(1,len(pairs)):if pairs[i,2] == pairs[i-1,2]:row[i] = row[i-1]else:row[i] = row[i-1]+1pairs[:,2] = row # # (列, 原始順序,行號(hào))pairs = pairs[np.argsort(pairs[:,1])] # 按原始順序復(fù)原df = pd.DataFrame({'c':c, 'x':x, 'r':pairs[:,2]})df.sort_values(by=['r','x'], inplace=True)row=-1result = ''for _, item in df.iterrows():if row==item[2]:result += ','else:row=item[2]result += '\n'result+=item['c'].encode('utf-8').decode('unicode_escape','ignore')print(result)breakexcept Exception as e:print(e.trace) KUKA ROBOT,輸入輸出信號(hào) 機(jī)種,AIMI-->>上料機(jī)器人電箱信號(hào),客戶,鍛造 信號(hào),CLEAN,AIMI,Robot,KUKA符號(hào),CLEANING符號(hào),注釋 輸入,輸出,輸出,輸入 IN1,Q60.0,DI001,Di01PrgCode1,程序號(hào)設(shè)定輸入,1 IN2,Q60.1,DI002,Di02PrgCode2,程序號(hào)設(shè)定輸入,2 IN3,Q60.2,DI003,Di03PrgCode3,程序號(hào)設(shè)定輸入,3 IN4,Q60.3,DI004,Di04PrgCode4,程序號(hào)設(shè)定輸入,4 IN5,Q60.4,DI005,Di05PrgCode5,程序號(hào)設(shè)定輸入,5 IN6,Q60.5,DI006,Di06PrgCode6,程序號(hào)設(shè)定輸入,6 IN7,Q60.6,DI007,Di07PrgCode7,程序號(hào)設(shè)定輸入,7 IN8,Q60.7,DI008,Di08PrgCode8,程序號(hào)設(shè)定輸入,8 ...總結(jié)
以上是生活随笔為你收集整理的baidu文库爪巴虫——xls的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 人生重开模拟器(Python实现)
- 下一篇: 融合办公时代来了?联想Filez带你正确