数据获取方案

在浏览数据中数据会自动发起请求(可通过Python模拟鼠标滚动实现自动化)
import pyautogui # pip install pyautogui
import time
import progressbar
widgets=['进度:',progressbar.Timer(),']',progressbar.Percentage(),progressbar.Bar(), '(',progressbar.ETA(),')']
bar=progressbar.ProgressBar(widgets=widgets)
for i in bar(range(2*12*60)):
pyautogui.scroll(-1000)
time.sleep(2)

将请求返回的数据另存为文件

数据解析
file=open('data/pharmsnap.zhihuiya.com.har',encoding='UTF-8')
text=file.read()
file.close()
import re
pattern=re.compile('"text":.+offset.+limit.+total.+items.+',re.M)
data=pattern.findall(text)

数据处理?

?

功能封装?
def To_DataFrame(item):
# 提取内容
xhr=item.replace('\\"','"').replace('true','True').replace('false','False')[8:]
dic=eval(xhr[1:-1])
offset=dic['offset']
limit=dic['limit']
total=dic['total']
items=dic['items']
df=pd.DataFrame(items)
df['offset']=offset
df['limit']=limit
df['total']=total
return df

df=pd.DataFrame()
for item in data:
df = pd.concat([df,To_DataFrame(item)])
?

保存数据?
df.to_excel('./data/pharmsnap.xlsx')
?
|