我正在使用以下脚本从视频网站中提取 10 个视频。每次运行时,它都会拉取相同的 10 个视频。相反,我希望它每次都能拉出 10 个不同的视频。我怎样才能做到这一点?
我一直在尝试添加videos = random.choice(video)其他内容。
def link(url):
headers = {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
videos = list(soup.find_all('script', type='application/ld+json', limit=10))
for video in videos:
if '.mp4' in video.string:
try:
video = json.loads(video.string.strip())
r = requests.get(video['contentUrl'], stream=True)
filename = video['contentUrl'].split('/')[-1]
with open("/Users/path/Desktop/Test/" + filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size=1024):
fd.write(chunk)
except Exception:
print(Exception)
link('somewebsite.com')
至尊宝的传说
蝴蝶不菲
相关分类