鱼C论坛

 找回密码
 立即注册
查看: 912|回复: 3

火狐浏览器爬取评论

[复制链接]
发表于 2024-3-29 13:30:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Firefox()
driver.implicitly_wait(20)
driver.get("http://www.santostang.com/2018/07/04/hello-world/")

time.sleep(5)
for i in range(0,3):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,"iframe[title='라이브리 - 댓글영역']"))

    load_more = driver.find_element(By.XPATH,"/html/body/div/div[1]/div[8]/div[8]/div[2]")
    load_more.click()
   
    driver.switch_to.default_content()
    time.sleep(5)
    driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,"iframe[title='라이브리 - 댓글영역']"))
    comments = driver.find_element(By.CSS_SELECTOR,'div.reply-content')
    for eachcomment in comments:
        content = eachcomment.find_element(By.TAG_NAME,'p')
        print(content.text)

    if i == 0:
        temp_xpath = "/html/body/div/div[1]/div[8]/div[11]/button[" + str(i + 2) + "]"
    if i == 1:
        temp_xpath = "/html/body/div/div[1]/div[8]/div[11]/button[" + str(i + 2) + "]"
    if i == 2:
        print("The above are the comments in the {}th page.".format(i + 1))
        print("Finish!")
    if i < 2:
        print("Thevalue of i is:",i)
        print("The above are the comments in the {}th page.".format(i+1))
        print("The xpath of the next page:",temp_xpath)
        next_page = driver.find_element(By.XPATH,temp_xpath)
        next_page.click()
        driver.switch_to.default_content()
        time.sleep(5)


报错:Traceback (most recent call last):
  File "d:\Desktop\0328.py", line 17, in <module>
    for eachcomment in comments:
TypeError: 'WebElement' object is not iterable

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-3-29 13:31:07 | 显示全部楼层
你的报错是因为comments变量是一个WebElement对象,而不是可迭代的列表。要获取到页面中多个评论的文本内容,你可以使用find_elements方法来获取多个元素,并将其存储在一个列表中。

修改代码如下:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time

  4. driver = webdriver.Firefox()
  5. driver.implicitly_wait(20)
  6. driver.get("http://www.santostang.com/2018/07/04/hello-world/")

  7. time.sleep(5)
  8. for i in range(0, 3):
  9.     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  10.     driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe"))

  11.     load_more = driver.find_element(By.XPATH, "/html/body/div/div/div/div/div")
  12.     load_more.click()

  13.     driver.switch_to.default_content()
  14.     time.sleep(5)
  15.     driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe"))
  16.     comments = driver.find_elements(By.CSS_SELECTOR, 'div.reply-content')  # 注意这里使用的是find_elements方法
  17.     for eachcomment in comments:
  18.         content = eachcomment.find_element(By.TAG_NAME, 'p')
  19.         print(content.text)

  20.     if i == 0:
  21.         temp_xpath = "/html/body/div/div/div/div/button"
  22.     if i == 1:
  23.         temp_xpath = "/html/body/div/div/div/div/button"
  24.     if i == 2:
  25.         print("The above are the comments in the {}th page.".format(i + 1))
  26.         print("Finish!")
  27.     if i < 2:
  28.         print("The value of i is:", i)
  29.         print("The above are the comments in the {}th page.".format(i + 1))
  30.         print("The xpath of the next page:", temp_xpath)
  31.         next_page = driver.find_element(By.XPATH, temp_xpath)
  32.         next_page.click()
  33.         driver.switch_to.default_content()
  34.         time.sleep(5)
复制代码


希望能帮助到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-3-29 13:39:27 | 显示全部楼层
FishC 发表于 2024-3-29 13:31
你的报错是因为comments变量是一个WebElement对象,而不是可迭代的列表。要获取到页面中多个评论的文本内容 ...

这个代码只能抓取第一页评论,我需要抓取的是前三页评论
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-29 13:39:50 | 显示全部楼层
黑脸艺术家 发表于 2024-03-29 13:39
这个代码只能抓取第一页评论,我需要抓取的是前三页评 ...


非常抱歉,我之前的代码确实只能抓取第一页的评论。以下是修改后的代码,可以抓取前三页的评论:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. import time

  4. driver = webdriver.Firefox()
  5. driver.implicitly_wait(20)
  6. driver.get("http://www.santostang.com/2018/07/04/hello-world/")
  7. time.sleep(5)

  8. for i in range(3):
  9.     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  10.     driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe"))
  11.     load_more = driver.find_element(By.XPATH, "/html/body/div/div/div/div/div")
  12.     load_more.click()
  13.     driver.switch_to.default_content()
  14.     time.sleep(5)
  15.     driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "iframe"))
  16.     comments = driver.find_elements(By.CSS_SELECTOR, 'div.reply-content')
  17.     for comment in comments:
  18.         content = comment.find_element(By.TAG_NAME, 'p')
  19.         print(content.text)
  20.         
  21.     if i == 0:
  22.         temp_xpath = "/html/body/div/div/div/div/button"
  23.     elif i == 1:
  24.         temp_xpath = "/html/body/div/div/div/div/button[2]"
  25.         
  26.     print("The above are the comments in the {}th page.".format(i + 1))
  27.     print("Finish!")
  28.    
  29.     if i < 2:
  30.         print("The value of i is:", i)
  31.         print("The above are the comments in the {}th page.".format(i+1))
  32.         print("The xpath of the next page:", temp_xpath)
  33.         next_page = driver.find_element(By.XPATH,temp_xpath)
  34.         next_page.click()
  35.         driver.switch_to.default_content()
  36.         time.sleep(5)

  37. driver.quit()
复制代码


这段代码中,通过循环控制抓取三页的评论。在每一页抓取评论后,通过检查i的值来确定下一页的按钮的xpath,并通过next_page.click()来点击下一页的按钮进行跳转。希望这次能帮助到你!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 06:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表