我是 Python 新手,正在使用 BeautifulSoup 練習網頁抓取。我檢查了一些類似的問題,例如這個、這個和這個。但是,我仍然陷入我的問題。這是我的代碼:import urllib.requestfrom bs4 import BeautifulSouphtml = urllib.request.urlopen("https://en.wikipedia.org/wiki/List_of_largest_recorded_music_markets").read()soup = BeautifulSoup(html, 'html.parser')tbody = soup.find_all('table',{"class":"wikitable plainrowheaders sortable jquery-tablesorter"})首先,我不認為我正在尋找的網頁包含類似問題中提到的 java 腳本。我打算提取這些表中的數據,但是當我執行 print(tbody) 時,我發現它是一個空列表。有人可以看看并給我一些提示嗎?謝謝你。
1 回答

慕村9548890
TA貢獻1884條經驗 獲得超4個贊
您必須刪除 jquery-tablesorter 部分。它是在頁面加載后動態應用的,所以如果你包含它,它就不起作用。
這應該有效:
import urllib.request
from bs4 import BeautifulSoup
html = urllib.request.urlopen("https://en.wikipedia.org/wiki/List_of_largest_recorded_music_markets").read()
soup = BeautifulSoup(html, 'html.parser')
tbody = soup.find('table', {"class": "wikitable plainrowheaders sortable"})
print(tbody)
添加回答
舉報
0/150
提交
取消