【问题标题】:Why I'm not getting complete div in BeautifulSoup python?为什么我没有在 BeautifulSoup python 中获得完整的 div?
【发布时间】:2021-10-12 13:26:56
【问题描述】:

我的代码

import requests
from bs4 import BeautifulSoup

URL = "https://www.quora.com/profile/Siddhartha-gaur-5"
r = requests.get(URL)
 
soup = BeautifulSoup(r.content,'html5lib')
for data in soup.find("div", id="root"): 
    print(data)

我得到了没有如下内容的空白 div 标签。

<div id="root"></div>

我想提取本示例中给出的所有内容:Complete div......

【问题讨论】:

  • 你应该使用硒。它正在动态加载。

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


【解决方案1】:

这是有效的代码

import requests
from bs4 import BeautifulSoup
from selenium import webdriver
# MAIN
driver = webdriver.Chrome('cromedriver path here')
driver.maximize_window()


#Go to link
URL = "https://www.quora.com/profile/Siddhartha-gaur-5"
driver.get(URL)

soup = BeautifulSoup(driver.page_source,'lxml')
driver.close()

for data in soup.find("div", id="root"): 
    print(data)

【讨论】:

  • 这段代码运行良好,但问题是我想托管我的脚本和使用 Web 驱动程序时遇到问题有没有其他方法可以做到这一点?
  • 是的,还有另一种方法,创建一个新的帖子,即 api 调用 json 响应。我想发布我的答案,但您的帖子已删除
  • 好的,我正在发帖,再次感谢您的回答:)
猜你喜欢
  • 2021-07-03
  • 2017-06-07
  • 2010-11-02
  • 2014-12-21
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多