【问题标题】:BeautifulSoup and Selenium cannot fetch <p> content under nested <div>BeautifulSoup 和 Selenium 无法获取嵌套 <div> 下的 <p> 内容
【发布时间】:2021-09-21 07:20:58
【问题描述】:

我正在尝试从网页中抓取评论。附图显示评论位于名为“more reviewdata”的 div 类下的 &lt;p&gt; 标签中。 我先使用 BeautifulSoup,然后使用 Selenium 提取“更多评论数据”部分,但失败了,尽管其他 &lt;p&gt;&lt;div&gt; 标签提取得很好。我访问的几个教程网站之一暗示动态页面不会通过单击检查来显示所有来源。但是这里点击Inspect后显示的是review内容,这意味着这个页面不是动态的。有没有人建议。提前致谢。对于 BeautifulSoup,我的代码是这样的:

import requests
url = 'https://www.mouthshut.com/hindi-movies/Tanhaji-reviews-925997893'
response = requests.get(url)
page_contents = response.text
from bs4 import BeautifulSoup
doc = BeautifulSoup(page_contents, 'html.parser')

对于我写的 Selenium 和 Chrome 驱动程序:

from selenium import webdriver    
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", options=options)
import time    
driver.get("https://www.mouthshut.com/hindi-movies/Tanhaji-reviews-925997893")
more_review_data_class = driver.find_elements_by_class_name("more reviewdata")
page_contents = driver.page_source

【问题讨论】:

  • 尝试类名more.reviewdata(注意中间的点)

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

如果有多个类名,您应该使用 css 选择器或 XPath。
所以不是

more_review_data_class = driver.find_elements_by_class_name("more reviewdata")

试试这个:

more_review_data = driver.find_elements_by_css_selector(".more.reviewdata p")

或者这个

more_review_data = driver.find_elements_by_xpath("//div[@class='more reviewdata']//p")

您还应该在访问元素之前添加一些等待以使页面加载。所以它会是这样的:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.more.reviewdata p")))
time.sleep(0.5)
more_review_data = driver.find_elements_by_css_selector(".more.reviewdata p")

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='more reviewdata']//p")))
time.sleep(0.5)
more_review_data = driver.find_elements_by_xpath("//div[@class='more reviewdata']//p")

要简单地打印元素内的文本,您可以迭代元素列表并像这样打印每个元素文本:

for element in more_review_data:
    print(element.text)

for element in more_review_data:
    print(element.get_attribute("innerHTML"))

【讨论】:

  • 我试过你的代码,它返回了这个:&lt;selenium.webdriver.remote.webelement.WebElement (session="4ed11678498c1cf0d68fa180b934fe85", element="1732f835-78ed-47da-aaa7-1090b939b702")&gt;。接下来我该怎么办?
  • 你试图做什么?它为您提供了一个 Web 元素本身,而不是其中的文本。您要提取文本还是其他内容?
  • 是的,我想提取其中的文本。我浏览了 find_element_ 函数列表,但不知道如何继续。
  • 我添加了打印文本的代码。请注意,我在定位器中添加了p 父级以获取p 元素本身,而不是父级div。两者都应该正确工作,但最好是准确地做。
  • for element in more_review_data:行中显示错误亲爱的TypeError: 'WebElement' object is not iterable
【解决方案2】:

当您加载网站内容时,评论是动态加载的,因此如果您转到 Developer modeNetwork tab 并查找与评论相关的数据链接,将显示哪些内容与网站相关的所有评论!。

代码:

import requests
res=requests.get("https://www.mouthshut.com/Review/rar_reviews.aspx?cname=Tanhaji&cid=925997893&movie=1")
soup=BeautifulSoup(res.text,"lxml")

这里我使用了返回数据列表的 css 类选择器

main_data=soup.select("div.more.reviewdata")
for i in main_data:
    print(i.find("p").get_text())

这是上面脚本的输出:

   The movie is on real fact there was war for Kondhana ghad Tanhaji Malusare had attack on mughul on  4th - Feb 1670 and the brave fighter Tanhaji's one hand was cutted by Udaybhan but they still fighting and The Maratha's win the war I love the film and the unity of sawarj also great described in the film 
. ....

图片:

【讨论】:

  • 我会要求这个答案的编辑恢复安静相关并有助于我们更好地理解这个想法的图像。
  • 好吧,让我添加它对不起我批准了它!
【解决方案3】:

你试过了吗?

driver.find_elements_by_xpath("//div[@class='class name']")

你的情况

driver.find_elements_by_xpath("//div[@class='more reviewdata']")

【讨论】:

  • 这看起来不像 python
  • 是的,修好了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 2021-09-07
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多