python - 用 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 驱动程序,但得到了类似的结果。
解决方案
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)
推荐阅读
- mongodb - MongoDB中的事务锁
- javascript - 在 Chrome 上的开发人员控制台中使用“require”运行 JavaScript
- nsis - 将 HKLM/HKCU 分配给不同类型安装的变量
- delphi - Delphi:我如何知道哪个索引属性具有使用 RTTI 的字符串索引
- java - Hibernate @OneToMany 映射 - 删除记录的问题
- php - php preg_match 可选斜杠无法正常工作
- django - PostgreSQL 通过 django 而不是 wsgi 连接,库没有密码
- php - 通过 jquery 生成的表单附加帖子
- apache-kafka - 使用 Flink 动态创建和更改 Kafka 主题
- c - 对于 OpenBSD 上的原始套接字 icmp,协议族不支持 sendto 地址族