数据采集与融合技术-第五次作业

发布时间:2022-06-26 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了数据采集与融合技术-第五次作业脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

作业①

  • 1.1作业内容

    • 要求:

      • 熟练掌握 Selenium 查找HTML元素、爬取Ajax网页数据、等待HTML元素等内容。
      • 使用Selenium框架爬取京东商城某类商品信息及图片。
    • 候选网站:http://www.jd.COM/

    • 输出信息:MySQL的输出信息如下

      mNomMarkmPRicemNotemFile
      000001 三星Galaxy 9199.00 三星Galaxy Note20 Ultra 5G... 000001.jpg
      000002......        
  • 1.2代码及实验步骤

    • 1.2.1实验步骤:

  复制xpath路径,传入关键字        

数据采集与融合技术-第五次作业


实现点击搜索按钮

@H_777_97@but = self.driver.find_element_by_xpath('//*[@id="seArch"]/div/div[2]/button')
but.click()
time.sleep(10)

实现滚动翻页

for i in range(33):
  self.driver.execute_script("VAR a = window.innerHeight;window.scrollBy(0,a*0.5);")
  time.sleep(0.5)

分析商品页面,每个商品的信息都在li标签中html = self.driver.find_elements_by_xpath('//*[@id="J_goodsList"]/ul/li')#爬取所有li标签


遍历每个li,爬取节点信息

for ITem in range(len(html)):
	try:
		mMark = html[item].find_element_by_xpath('./div//div[@class="p-name"]/a/em/font[1]').text
		print(mMark)
	except Exception as err:
		mMark = " "
	mPrice = html[item].find_element_by_xpath('./div//div[@class="p-price"]/strong/i').text
	mNote = html[item].find_element_by_xpath('./div//div[@class="p-name"]/a/em').text
	src = html[item].find_element_by_xpath('./div//div[@class="p-img"]/a/img').get_attribute('src')
	self.picSave(src)
	self.db.insert(self.count, mMark, mPrice, mNote, str(self.count)+".jpg")
	self.count += 1

实现翻页

if self.page < 2:
	self.page += 1
	nextPage = self.driver.find_element_by_xpath('//*[@id="J_bottomPage"]/span[1]/a[9]')
	nextPage.click()
	#再次执行爬取函数
	self.Mining()

  • 1.3运行结果:

数据采集与融合技术-第五次作业

 

  • 1.4心得体会

    • 爬取信息时先爬取每个商品的li节点,再循环爬取信息节点
    • 学会了模拟搜索
    • 在爬取过程中必须注意设置sleep时间,以便网页完成加载

作业②

  • 2.1作业内容

    • 要求:

      • 熟练掌握 Selenium 查找HTML元素、实现用户模拟登录、爬取Ajax网页数据、等待HTML元素等内容。
      • 使用Selenium框架+MySQL模拟登录慕课网,并获取学生自己账户中已学课程的信息保存到MySQL中(课程号、课程名称、授课单位、教学进度、课程状态,课程图片地址),同时存储图片到本地项目根目录下的imgs文件夹中,图片的名称用课程名来存储。
    • 候选网站:中国mooc网:https://www.icourse163.org

    • 输出信息:MySQL数据库存储和输出格式

      IdcCoursecCollegecSchedulecCoursestatuscImgUrl
      1 Python网络爬虫与信息提取 北京理工大学 已学3/18课时 2021年5月18日已结束 http://edu-image.nosdn.127.net/C0AB6FA791150F0Dfc0946B9A01C8CB2.jpg
      2......          
  • 2.2代码及实验步骤

    • 2.2.1实验步骤
      # 登录入口
      DL = self.driver.find_element_by_xpath('//*[@id="app"]/div/div/div[1]/div[3]/div[3]/div')
      DL.click()
      # 点击其他方式登录
      QTDL = self.driver.find_element_by_xpath('//span[@class="ux-login-set-scan-code_ft_back"]')
      QTDL.click()
      # 点击手机登录
      phoneDL = self.driver.find_element_by_xpath('//ul[@class="ux-tabs-underline_hd"]/li[2]')
      phoneDL.click()
      # 切换浮动窗口
      phoneI = self.driver.find_element_by_xpath('//div[@class="ux-LOGin-set-container"][@id="j-ursContainer-1"]/iframe')
      self.driver.switch_to.frame(phoneI)
      # 输入手机号
      phoneNum = self.driver.find_element_by_xpath('//*[@id="phoneipt"]')
      phoneNum.send_keys(USERNAME)
      # 输入密码
      phonePassword = self.driver.find_element_by_xpath('//div[@class="u-input box"]/input[2]')
      phonePassword.send_keys(PASSWORD)
      # 点击登录,等待登录成功
      DlClick = self.driver.find_element_by_xpath('//*[@id="submitBTn"]')
      DlClick.click()
      time.sleep(10)
      # 进入我的课程
      myClass = self.driver.find_element_by_xpath('//div[@class="_1Y4Ni"]/div')
      myClass.click()
      time.sleep(5)

分析课程页面,每个课程的信息都在一个div标签中

数据采集与融合技术-第五次作业

  # 爬取每个课程的div标签
  html = self.driver.find_elements_by_xpath('//*[@id="j-coursewrap"]/div/div/div')

遍历每个div,爬取节点信息

# 爬取信息
for item in html:
    print(self.count)
    cCourse = item.find_element_by_xpath('./div//div[@class="text"]/span[@class="text"]').text
    print(cCourse)
    cCollege = item.find_element_by_xpath('./div//div[@class="school"]/a').text
    print(cCollege)
    cSchedule = item.find_element_by_xpath('./div//div[@class="text"]/a/span').text
    print(cSchedule)
    cCourseStatus = item.find_element_by_xpath('./div//div[@class="course-status"]').text
    print(cCourseStatus)
    src = item.find_element_by_xpath('./div//div[@class="img"]/img').get_attribute("src")
    print(src)
    self.picSave(src, cCourse)
    self.db.insert(self.count, cCourse, cCollege, cSchedule, cCourseStatus, src)
    self.count += 1

实现翻页

nextPage = self.driver.find_element_by_xpath('//*[@id="j-coursewrap"]/div/div[2]/ul/li[4]/a')
# 实现翻页
if nextPage.get_attribute('class') != "th-bk-disable-gh":
    print(1)
    nextPage.click()
    time.sleep(5)
    # 再次执行爬取函数
    self.Mining()


  • 2.3运行结果:

数据采集与融合技术-第五次作业

 

 

 

 

作业③

    • 3.1作业内容

      • 要求:掌握大数据相关服务,熟悉Xshell的使用
        • 完成文档 华为_大数据实时分析处理实验手册-Flume日志采集实验(部分)v2.docx 中的任务,即为下面5个任务,具体操作见文档。
        • 环境搭建
          • 任务一:开通Mapreduce服务
        • 实时分析开发实战:
          • 任务一:Python脚本生成测试数据
          • 任务二:配置Kafka
          • 任务三:安装Flume客户端
          • 任务四:配置Flume采集数据
    • 3.2结果

      • 任务一:Python脚本生成测试数据执行python文件

        数据采集与融合技术-第五次作业

        查看生成数据

      • 任务二:配置Kafka执行source

        数据采集与融合技术-第五次作业

        任务三:安装Flume客户端

      • 最后安装Flume

        数据采集与融合技术-第五次作业

        重启服务

        数据采集与融合技术-第五次作业

      • 任务四:配置Flume采集数据

脚本宝典总结

以上是脚本宝典为你收集整理的数据采集与融合技术-第五次作业全部内容,希望文章能够帮你解决数据采集与融合技术-第五次作业所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。