使用BeautifulSoup分析网页时不会给出完整的页面内容

2024-05-17 17:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析这个网页上的描述“享受创造和控制的力量…”: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驱动程序,但得到了类似的结果。你知道吗


Tags: thecom网页searchpage页面originquery
1条回答
网友
1楼 · 发布于 2024-05-17 17:34:39

请求和BeautifulSoup对此不起作用,因为页面是用javascript动态加载的。这就是为什么你找不到描述。SeleniumWebDriver应该可以正常工作。我写了一些代码来获取描述。你知道吗


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)

相关问题 更多 >