【问题标题】:Parsing Webpage with BeautifulSoup Doesn't Give Full Page Contents用 BeautifulSoup 解析网页不会给出整页内容
【发布时间】:2019-10-23 21:42:48
【问题描述】:

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

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    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)
    
    

    【讨论】:

    • 感谢您对 LuckyZakary 的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2012-10-04
    • 2013-04-23
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多