Skip to main content
 首页 » 编程设计

python之使用 BeautifulSoup 解析网页不会给出完整的页面内容

2024年11月24日7youxin

我正在尝试从该网页解析“享受创造和控制的力量……”的描述:https://www.origin.com/zaf/en-us/store/the-sims/the-sims-4 .

当我用 Beautifulsoup 解析页面时,页面源不包含描述,我不确定为什么。

handle = 'sims 4' 
 
query = handle + " origin.com"  # enter query to search 
print(query) 
for topresult in search(query, tld="com", lang='en', num=10, stop=1, pause=2):   
    print('Query Successful:' + handle) 
 
page = requests.get(topresult) 
soup = BeautifulSoup(page, 'html.parser') 
 
print(soup) 

如有任何帮助,我们将不胜感激。几天来我一直在努力解决这个问题。我也尝试过使用 Selenium 和 Chrome 驱动程序,但得到了类似的结果。

请您参考如下方法:

Requests 和 BeautifulSoup 对此不起作用,因为页面是使用 javascript 动态加载的。这就是为什么您找不到描述的原因。 Selenium webdriver 应该可以正常工作。我写了一些代码来获取描述。


from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
 
driver = webdriver.Chrome() 
 
driver.get('https://www.origin.com/zaf/en-us/store/the-sims/the-sims-4') 
desc = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//p[@ng-bind-html="::$ctrl.description"]'))) 
print(desc.text)