新來美麗的湯4,當我在YouTube上搜索某些東西時,我無法獲取這個簡單的代碼來獲取標籤的內容。當我打印容器時,它只是打印「[]」作爲我假設的一個空變量。任何想法,爲什麼這不是挑選什麼?這是否與不在YouTube上抓取正確的標籤?在搜索HTML有一個結果如下標籤:爲什麼find_all BeautifulSoup4函數什麼都不返回?
<a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" aria-label="Kendrick Lamar - HUMBLE. by KendrickLamarVEVO 5 months ago 3 minutes, 4 seconds 322,571,817 views" href="https://www.youtube.com/watch?v=tvTRZJ-4EyI" title="Kendrick Lamar - HUMBLE.">
Kendrick Lamar - HUMBLE.
</a>
Python代碼:
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
search = "damn"
my_url = "https://www.youtube.com/results?search_query=" + search
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html, "html.parser")
containers = page_soup.find_all("a",{"id":"video-title"})
print(containers)
#result-count
這裏工作很好。你是否檢查過「page_html」包含你所期望的內容? (另外,'page_soup.find(id ='video-title')'會更簡單。) – Ryan
page_html中似乎沒有'id =「video-title」'的'',如果你想要頁面的結果使用'page_soup.find_all('a',{'class':'yt-uix-sessionlink spf-link'})'。 – Bijoy