from fileinput import filename from lib2to3.pgen2 import driver from xml.etree.ElementInclude import include from selenium import webdriver from bs4 import BeautifulSoup import time from selenium.webdriver.common.by import By from bs4 import BeautifulSoup import requests class Downloader(object): def __init__(self, url): self.url = url self.urlist = [] self.DownloadUrl() # 下载链接 def DownloadUrl(self): driver = webdriver.Chrome() driver.maximize_window() driver.get(url) time.sleep(5) sp = driver.find_elements(By.XPATH, "//video[@class='wbpv-tech']").get_attribute("src") print("开始保存链接……%s" % sp) self.SavePicture(sp) # 保存图片到本地 def SavePicture(self, sp): driver2 = webdriver.Chrome() driver2.maximize_window() time.sleep(5) img = driver2.get(sp) with open("photo.mp4", "wb") as f: f.write(requests.get(sp).content) print("保存完成") if __name__ == "__main__": url = 'https://s.weibo.com/weibo?q=%23%E5%A4%A9%E5%92%8C%E8%A7%86%E8%A7%92%E4%B8%8B%E7%9A%84%E7%A5%9E%E5%8D%81%E4%B8%89%E6%92%A4%E7%A6%BB%23&Refer=top' Downloader(url)
时间: 2024-04-26 17:25:52 浏览: 84
bootstrap-fileinput-master.zip
这段代码的作用是下载微博页面中的视频。它使用了Selenium和BeautifulSoup库,通过打开浏览器获取页面元素,然后再使用requests库将视频文件保存到本地。其中,class Downloader 是一个包含了初始化函数和下载链接函数的类,它在初始化时传入一个url参数,并在 DownloadUrl 函数中通过使用 Selenium 打开浏览器获取视频的链接,最后在 SavePicture 函数中使用 requests 库将视频文件保存到本地。如果你想要使用这段代码,需要先安装好相关的库,并且将浏览器驱动程序放到 PATH 环境变量中。
阅读全文