因此,我正在尝试通过beautifulsoup获取youtube上热门页面的标题。一般来说,使用python,beautifulsoup和stackexchange进行处理真的是我的新手,对不起任何错误和处理。
我尝试看了一些视频,但是我找不到很多
from bs4 import BeautifulSoup
from urllib.request import urlopen
import requests
url = 'https://youtube.com'
rq = requests.get(url).text
soup = BeautifulSoup(rq, 'html.parser')
trending = soup.find_All('div', id="dismissable")
print(trending)
我想获取YouTube趋势的HTML,以便我可以搜索每个特定的标题。我有一个空东西。是[]或无。
答案 0 :(得分:0)
from bs4 import BeautifulSoup
import requests
import csv
source = requests.get("https://www.youtube.com/feed/trending").text
soup = BeautifulSoup(source, 'lxml')#.text.encode("utf-8")
csv_file = open('YouTube Trending Titles on 12-30-18.csv','w')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Title', 'Description'])
for content in soup.findAll('div', class_= "yt-lockup-content"):
#print (content)
try:
title = content.h3.a.text
print(title)
description = content.find('div', class_="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2").text
print(description)
except Exception as e:
description = None
print('\n')
csv_writer.writerow([title.encode('utf8') , description.encode('utf8') ])
csv_file.close()