【问题标题】:bs4 does not return full HTMLbs4 不返回完整的 HTML
【发布时间】:2021-12-15 08:43:30
【问题描述】:

我正在尝试使用 bs4 和请求从网站获取一些信息。

网址是:https://www.element14.com/community/community/design-challenges/in-the-air-design-challenge/blog/2014/10/26/firecracker-analyzer-index

我正在尝试访问特定的 div:

<div id="jive-comment-tabs" class="j-comment-wrapper" xmlns="http://www.w3.org/1999/html"> ..... </div>

但是当我使用以下代码时:

import requests
from bs4 import BeautifulSoup


URL = "https://www.element14.com/community/community/design-challenges/in-the-air-design-challenge/blog/2014/10/26/firecracker-analyzer-index"            
page = requests.get(URL)
soup = BeautifulSoup(page.content, "lxml")
print(soup.find('div', {'class': 'j-comment-wrapper'}))

我得到 None 结果,我知道它在网页上。我尝试了互联网上的大多数解决方案,但没有一个对我有帮助。有什么想法吗?

【问题讨论】:

  • 您的代码对我有用。你检查响应状态码了吗?

标签: python-3.x web-scraping beautifulsoup python-requests


【解决方案1】:

会发生什么?

网站正在动态地提供这部分内容,因此您不会以这种方式通过请求获得它。

替代方法

尝试使用selenium,它会渲染页面,你会得到你的结果。

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('YOUR PATH TO CHROMEDRIVER')
driver.get('https://www.element14.com/community/community/design-challenges/in-the-air-design-challenge/blog/2014/10/26/firecracker-analyzer-index')

soup=BeautifulSoup(driver.page_source, 'html.parser')

soup.find('div', {'class': 'j-comment-wrapper'})

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2019-07-21
    • 2011-05-23
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多