-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdanmu.py
47 lines (35 loc) · 1.04 KB
/
danmu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! -*- coding:utf-8 -*-
"""
Author:Lz1y
2017.6.6
"""
import re
import requests
from bs4 import BeautifulSoup as bs
def getAv():
try:
av = input("请输入Av号:")
url = "http://www.bilibili.com/video/av" + str(av) + "/"
getHTMLText(url, av)
except:
print("输入错误.")
def getHTMLText(url, av):
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)", }
u = requests.get(url=url, headers=headers)
html = u.text
cid = re.findall(r'cid=(.*?)&aid=', html)[0]
getDanmu(cid, av)
def getDanmu(cid, av):
dmurl = "http://comment.bilibili.com/" + str(cid) + ".xml"
dmhtml = requests.get(dmurl).text
soup = bs(dmhtml, 'xml')
dmlist = soup.find_all('d')
printDanmu(dmlist, av)
def printDanmu(dmlist, av):
filename = "av" + str(av) + ".txt"
with open(filename, 'w', encoding='utf-8') as t:
for dm in dmlist:
t.write(str(dm.string) + '\n')
print("Loading...\n")
if __name__ == "__main__":
getAv()