1. 程式人生 > 實用技巧 >利用beautifulsoup進行對標籤的二次查詢

利用beautifulsoup進行對標籤的二次查詢

參考:https://www.cnblogs.com/bosslv/p/8992410.html

以網易雲歌單為例:https://music.163.com/#/playlist?id=3212113629

首先獲取頁面html

import requests
from bs4 import BeautifulSoup

url = "https://music.163.com/playlist?id=3212113629"    # 注意直接複製的地址有個#,這裡要去掉
demo = requests.get(url).text
soup = BeautifulSoup(demo, "html.parser")

通過列印soup可以看到要下載的內容在ul標籤

for ss in soup.find_all('ul'):
    print(ss)

執行會發現,有許多ul標籤

可以進行二次查詢

for ss0 in soup.find_all('ul',{"class":"f-hide"}):
    print(ss0)

完整程式碼為:

# write by xdd1997  [email protected]
# 2020-08-07

import requests
from bs4 import BeautifulSoup

url = "https://music.163.com/playlist?id=3212113629
" demo = requests.get(url).text soup = BeautifulSoup(demo, "html.parser") for ss0 in soup.find_all('ul',{"class":"f-hide"}): for ii in ss0.find_all('a'): print(ii.string)