Python 如何获取div标记的内容?
我试图用Python 如何获取div标记的内容?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我试图用Beautifulsoup从我评论的帖子中获取信息 但我觉得我很难掌握基本知识: from bs4 import BeautifulSoup import requests q_id = 27606129 res = requests.get(f"https://stackoverflow.com/review/first-posts/{q_id}") soup = BeautifulSoup(res.text, "html.parser") c
Beautifulsoup
从我评论的帖子中获取信息
但我觉得我很难掌握基本知识:
from bs4 import BeautifulSoup
import requests
q_id = 27606129
res = requests.get(f"https://stackoverflow.com/review/first-posts/{q_id}")
soup = BeautifulSoup(res.text, "html.parser")
content = soup.select(".review-content")
print(content)
输出:
[<div class="review-content"></div>]
标签,但它不返回任何内容。该网站是动态加载的,因此
请求
不支持它。我们可以用它来代替刮削页面
安装时使用:pip Install selenium
从下载正确的ChromeDriver
编辑:选择计票:
vote_count = soup.select_one("div[itemprop='upvoteCount']").text
您给出了两个不同的html代码?
from time import sleep
from selenium import webdriver
from bs4 import BeautifulSoup
q_id = 27606129
url = "https://stackoverflow.com/review/first-posts/{}"
driver = webdriver.Chrome(r"C:\path\to\chromedriver.exe")
driver.get(url.format(q_id))
# Wait for the page to fully render
sleep(5)
soup = BeautifulSoup(driver.page_source, "html.parser")
content = soup.select(".review-content")
print(content)
driver.quit()
vote_count = soup.select_one("div[itemprop='upvoteCount']").text