用PYTHON写一个所有视频网站的爬虫代码
时间: 2023-07-14 17:11:53 浏览: 94
在这里提供一个基于Python的爬虫脚本,可以用于爬取所有视频网站的视频信息:
```python
import requests
from bs4 import BeautifulSoup
# 定义函数获取视频列表
def get_video_list(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
videos = soup.find_all('a', {'class': 'video'})
video_list = []
for video in videos:
video_title = video.find('span', {'class': 'title'}).text
video_url = video['href']
video_list.append({'title': video_title, 'url': video_url})
return video_list
# 定义函数获取视频详情
def get_video_detail(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
video_title = soup.find('h1', {'class': 'title'}).text
video_desc = soup.find('div', {'class': 'desc'}).text.strip()
video_info = {'title': video_title, 'desc': video_desc}
return video_info
# 测试代码
if __name__ == '__main__':
url = 'https://www.example.com/videos/'
video_list = get_video_list(url)
for video in video_list:
video_detail = get_video_detail(video['url'])
print('标题:', video_detail['title'])
print('简介:', video_detail['desc'])
```
需要注意的是,不同的视频网站可能有不同的网页结构和接口,所以需要根据具体情况进行修改。同时,爬取视频网站的内容需要遵循网站的相关规定和法律法规,不得进行侵犯他人权益的行为。
阅读全文