鱼C论坛

 找回密码
 立即注册
查看: 779|回复: 1

爬虫相关--不懂就问

[复制链接]
发表于 2019-4-25 14:25:03 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 嗷呜呜呜 于 2019-4-25 14:26 编辑

想问下大家,在用selenium对动态页面进行爬取时,整个页面内容是放在子Frame里面的,页面打开子Frame爬到数据后,点击<下一页>的按钮也存放在子Frame里
如果想要爬取下一页的数据
①我需要在第一页先点击<下一页>按钮再返回父Frame然后在进行下一页的爬取;
②也就是我需要每爬一页都返回下一页的父Frame,再从下一页的父Frame中提取子Frame进行数据的提取;
③还是直接在子Frame点击<下一页>按钮之后等待页面自动跳转;
以网易云评论为例,下面代码只爬到第一页评论,已验证len(iframe)=1,
错误提示:iframe无法被点击,主要错误信息在open_page(url)函数中
想问下大家,如果下一页按钮也存放在子iframe中,怎么进行下一页评论内容的爬取
  1. from lxml import etree
  2. import time
  3. import random
  4. from selenium import webdriver
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from selenium.webdriver.common.by import By
  7. from selenium.webdriver.support import expected_conditions as EC
  8. # from selenium.common.exceptions import NoSuchElementException


  9. #####driver.switch_to_default_content()每次切换iframe返回父iframe

  10. all_contents = []

  11. def open_page(url):#打开页面
  12.     browser = webdriver.Chrome()
  13.     browser.get(url)
  14.     times = [0.8,1,1.2,1.3,1.5,0.9,0.5]
  15.     # browser.switch_to.frame('g_iframe')
  16.     for i in range(5):#五页数据
  17.         browser.switch_to.frame('g_iframe')#按照iframe中id的值来确定选取的iframe
  18.         text = browser.page_source
  19.         html = etree.HTML(text)
  20.         parse_page(html)
  21.         time.sleep(random.choice(times))
  22.         # browser.switch_to.parent_frame()
  23.         # time.sleep(0.5)
  24.         button = browser.find_element_by_xpath("//div[@class='m-cmmt']/div[last()]/div/a[last()]")
  25.         time.sleep(random.choice(times))
  26.         button.click()
  27.         wait = WebDriverWait(browser,10).until(EC.presence_of_all_elements_located((By.ID,"g_iframe")))

  28. def parse_page(html):#爬取详情
  29.     divs = html.xpath("//div[@class='cmmts j-flag']/div")
  30.     names = []
  31.     contents = []
  32.     response_names = []
  33.     response_contents = []
  34.     for div in divs:
  35.         name = div.xpath(".//div[@class='cnt f-brk']/a/text()")[0]
  36.         names.append(name)
  37.         content = div.xpath(".//div[@class='cnt f-brk']/text()")
  38.         content = "".join(content)
  39.         content = content.replace("\xa0", "").strip()
  40.         contents.append(content[1:])
  41.         response_name = div.xpath(".//div[@class='que f-brk f-pr s-fc3']/a[@class='s-fc7']/text()")
  42.         if response_name == []:
  43.             response_name.append("无回复者")
  44.         response_name = "".join(response_name)
  45.         response_content = div.xpath(".//div[@class='que f-brk f-pr s-fc3']/text()")
  46.         if response_content == []:
  47.             response_content.append("无回复内容")
  48.             response_content = "".join(response_content)
  49.         else:
  50.             response_content = "".join(response_content)
  51.             response_content = response_content[1:]
  52.         response_contents.append(response_content)
  53.         response_names.append(response_name)
  54.     all_content = list(zip(names,contents,response_names,response_contents))
  55.     for each_content in all_content:
  56.         names, contents, response_names, response_contents=each_content
  57.         contentses = {
  58.             "用户名":names,
  59.             "评论内容":contents,
  60.             "被回复用户":response_names,
  61.             "被回复评论":response_contents
  62.         }
  63.         all_contents.append(contentses)
  64.     for each in all_contents:
  65.         print(each, end="\n")

  66. def main():
  67.     url = "https://music.163.com/#/playlist?id=2703033645"
  68.     open_page(url)
  69.     for each in all_contents:
  70.         print(each,end = "\n")

  71. if __name__ == '__main__':
  72.     main()
复制代码



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

使用道具 举报

发表于 2019-4-26 09:45:27 | 显示全部楼层
目测是你睡眠时间设置的太短,网页还没有加载出来你就点击了操作,导致找不到相对应的元素,建议设置等待时间3秒以上。

上一句话是瞎蒙的,因为我并没有仔细看你的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-11 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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