直接复制标题的Xpath,注意将上一层的div[33]改成div[ * ] ,以便获取所以的标题数据
//*[@id=“app”]/div[4]/div[2]/div/div[6]/div[2]/div/div[1]/div[33]/div/div[1]
//[@id=“app”]/div[4]/div[2]/div/div[6]/div[2]/div/div[1]/div[]/div/div[1]
from selenium import webdriver
# 保存文件,传入参数:fin_data() 返回的列表
def save(video_list):
file_title = str(url_title) + ".txt" # 合成.txt格式 文件名
name_file = open(file_title, "w", encoding="utf-8") # 写入文件
for i in video_list:
name_file.write(i.text+"\n")
name_file.close()
#爬取的视频网址
url = "https://www.bilibili.com/video/BV183411P7xL/?spm_id_from=333.999.0.0"
#加载引擎
wd = webdriver.Edge()
#打开网址
wd.get(url)
wd.implicitly_wait(1)
#获取数据
name_list = wd.find_elements_by_xpath('//*[@id="app"]/div[4]/div[2]/div/div[6]/div[2]/div/div[1]/div[*]/div/div[1])
# 遍历列表
for i in name_list:
print(i.text)
# save(name_list)