首页 > 解决方案 > 用 BeautifulSoup 解析网页不会给出整页内容

问题描述

我正在尝试从该网页解析“享受创造和控制的力量......”的描述: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 驱动程序,但得到了类似的结果。

标签: pythonseleniumbeautifulsoup

解决方案


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)


推荐阅读