根据参考代码:
# 爬虫相关模块
import requests
import xlwt # 进行excel操作
def main():
global url
url ='地址'
#爬取网页
datalist =workData()
#保存数据
savepath = "ele.xls"
saveData(datalist,savepath)
#用来请求拿到数据
def getHTMLtext(url):
try:
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36",
"Cookie": "Cookie"
} # 浏览器请求头
r = requests.get(url, headers=headers, timeout=30) # 获取连接
electrics =r.json()['Data']
return electrics
except:
return ""
# 解析提取数据
def workData():# 解析html提取数据
datalist = []
for i in getHTMLtext(url):
# print(i)
data=[]
useDate = i['Date']
data.append(useDate)
useInitpower= i['Initpower']
data.append(useInitpower)
useLastpower= i['Lastpower']
data.append(useLastpower)
useAllmoney= i['Allmoney']
data.append(useAllmoney)
datalist.append(data)
return datalist
#将提取的数据写入excel
def saveData(datalist, savepath):
print("save.......")
book = xlwt.Workbook(encoding="utf-8", style_compression=0) # 创建workbook对象
sheet = book.add_sheet("电费", cell_overwrite_ok=True) # 创建工作表
col = ("useDate", "useInitpower","useLastpower","useAllmoney")
for i in range(0, 4):
sheet.write(0, i, col[i]) # 列名
for i in range(0, len(getHTMLtext(url))): #循环几次取决于返回了几个列表
data = datalist[i]
for j in range(0, 4):
sheet.write(i + 1, j, data[j]) # 数据
book.save(savepath) # 保存
main()
print("爬取完毕")
写的:
# 爬虫相关模块
import requests
import xlwt # 进行excel操作
#用来请求拿到数据
def getHTMLtext(url):
headers = {
"user-agent": "隐藏"
} # 浏览器请求头
r = requests.get(url, headers=headers, timeout=30) # 获取连接
herbdata =r.json()
return herbdata
global url
# 将提取的数据写入excel
book = xlwt.Workbook(encoding="utf-8", style_compression=0) # 创建workbook对象
sheet = book.add_sheet("herb草药", cell_overwrite_ok=True) # 创建工作表
col = ("id", "pinyinName", "englishName", "latinName")
for t in range(0, 4):
sheet.write(0, t, col[t]) # 列名
for page in range(1, 1000):
# 爬取网页
url=f"http://隐藏.aimicrobiome.cn/api/watson/queryHerb?id={page}"
# 解析提取数据 # 解析html提取数据
datalist = []
for i in getHTMLtext(url):
# print(i)
data = []
id = i['id']
print(id)
data.append(id)
pinyinName = i['pinyinName']
data.append(pinyinName)
englishName = i['englishName']
data.append(englishName)
latinName = i['latinName']
data.append(latinName)
datalist.append(data)
print(datalist)
# 保存数据
savepath = "herb.xls"
print("save.......")
data = datalist[0]
for j in range(0, 4):
sheet.write(page, j, data[j]) # 数据
book.save(savepath) # 保存
print("爬取完毕")