本帖最后由 Paakchiu 于 2019-11-23 17:02 编辑
各位大佬好,Python小白最近遇上问题,就是如下代码能跑起来,但是无法输出表格,想问下各位哪里有错,应该怎么改?谢谢各位的解答!
[Python] 纯文本查看 复制代码
from bs4 import BeautifulSoup
import re
import requests
import xlsxwriter
def getcomments():
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5558.400 QQBrowser/10.1.1695.400"}
url="https://cmts.iqiyi.com/bullet/30/00/7564083000_300_1.z?rn=0.21263356332144867&business=danmu&is_iqiyi=true&is_video_page=true&tvid=7564083000&albumid=233592301&categoryid=2&qypid=01010021010000000000"
html=requests.get(url,headers=headers)
bullets_data=html.content
bullets=decompress(bullets_data)
danmu=bullets.decode("utf-8","ignore")
soup=BeautifulSoup(danmu,"lxml")
workbook=xlsxwriter.Workbook(file_excel)
sheet=workbook.add_worksheet('弹幕文件')
bold=workbook.add_format({'bold':True})
sheet.write('A1',u'正文',bold)
sheet.write('I1',u'时间',bold)
sheet.write('L1',u'集数',bold)
bulletInfo=soup.select('bulletInfo')
row=2
for x in bulletInfo:
content=x.content.text
name="世界欠我一个初恋"
times=x.showtime.text
time=str(int(eval(times)/60))+"分"+str(eval(times)-int(eval(times)/60)*60)+"秒"#分进制 eval函数转化数据格式变为可计算
sheet.write('A%d'%row,content)
sheet.write('I%d'%row,time)
sheet.write('L%d'%row,name)
row+=1
workbook.close()
if __name__== '__main__':
file_excel="E:\Study\hiphop-1.xlsx"
getcomments()
print("我结束了")
谢谢各位大佬的意见,decompress是Python的函数还是我自己定义的函数?如果是Python的,我该如何调用;如果是自定义的,有没有大佬会写(Python小白给跪了;主要是用来解析该视频网站的弹幕包) |