Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取div标记的内容?_Python_Html_Beautifulsoup - Fatal编程技术网

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