1 回答

TA貢獻1802條經驗 獲得超5個贊
這是一個經典案例,如果您嘗試使用像 BeautifulSoup 這樣的爬蟲直接抓取網站,您將找不到任何東西。
該網站的工作方式是,首先將初始代碼塊下載到您的瀏覽器,就像您添加的一樣,big pencil
然后通過 Javascript,加載頁面上的其余元素。
您需要先使用Selenium Webdriver加載頁面,然后從瀏覽器中獲取代碼。在正常意義上,這相當于您打開瀏覽器的控制臺,轉到“元素”選項卡并查找您提到的類。
要查看差異,我建議您查看頁面的源代碼并與“元素”選項卡中的代碼進行比較
在這里,您需要使用 BS4 獲取加載到瀏覽器的數據
from selenium import webdriver
browser = webdriver.Chrome("path_to_chromedriver") # This is the Chromedriver which will open up a new instance of a browser for you. More info in the docs
browser.get(url) # Fetch the URL on the browser
soup = bs4.BeautifulSoup(browser.page_source, 'html.parser') # Now load it to BS4 and go on with extracting the elements and so on
這是了解 Selenium 的非?;镜拇a,但是,在生產用例中,您可能需要使用像PhantomJS這樣的無頭瀏覽器
添加回答
舉報