数据采集与融合技术实验四

发布时间 2023-11-04 15:26:32作者: Chen'coke

作业①:

  • 要求:使用 Selenium 框架+ MySQL 数据库存储技术路线爬取“沪深 A 股”、“上证 A 股”、“深证 A 股”3 个板块的股票数据信息。

  • 主要代码

url_list = ['http://quote.eastmoney.com/center/gridlist.html#hs_a_board','http://quote.eastmoney.com/center/gridlist.html#sh_a_board','http://quote.eastmoney.com/center/gridlist.html#sz_a_board']
con,cursor = openDB()
j = 0
for i in url_list:
    driver = webdriver.Chrome()  # 创建driver对象
    driver.get(i)
    time.sleep(10)
    driver.maximize_window()
    html = driver.page_source
    soup = BeautifulSoup(html, "lxml")
    imagelist = []
    lis = soup.select('table[class="table_wrapper-table"] tr')
    x = 1
    for ls in lis:
        j += 1
        if x != 1:
            data = ls.select('td')
            insert(cursor,str(j), data[1].text, data[2].text, data[4].text, data[5].text, data[6].text, data[7].text,
                  data[8].text, data[9].text, data[10].text, data[11].text, data[12].text, data[13].text)
            # print(data[0].text, data[1].text, data[2].text, data[4].text, data[5].text, data[6].text, data[7].text,
            #       data[8].text, data[9].text, data[10].text, data[11].text, data[12].text, data[13].text)
        else:
            # 排除表头
            x += 1
    driver.close()
show(cursor)
closeDB(con)
  • 输出信息:

  • Gitee文件夹链接
    题一

  • 心得体会
    对于不同的板块可以使用js的点击模块,也可以直接添加入网址列表。

作业②

  • 要求:使用 Selenium 框架+MySQL 爬取中国 mooc 网课程资源信息(课程号、课程名
    称、学校名称、主讲教师、团队成员、参加人数、课程进度、课程简介)

  • 主要代码

suser = '***'
spassword = '***'
con,cursor = openDB()
driver = webdriver.Chrome()  # 创建driver对象
driver.get('https://www.icourse163.org/')
driver.maximize_window()
time.sleep(5)

inputs = driver.find_element(By.CSS_SELECTOR, 'div[class=_1Y4Ni] div[class="_3uWA6"]')
inputs.click()
frame = driver.find_element(By.XPATH,'/html/body/div[13]/div[2]/div/div/div/div/div/div[1]/div/div[1]/div[2]/div[2]/div[1]/div/iframe')
driver.switch_to.frame(frame)
time.sleep(5)
user = driver.find_element(By.ID, 'phoneipt')
user.send_keys(suser)
password = driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/form/div/div[4]/div[2]/input[2]')
password.send_keys(spassword)

doit =  driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div[2]/form/div/div[6]/a')
doit.click()
time.sleep(3)
driver.switch_to.default_content()

name = driver.find_element(By.XPATH, '/html/body/div[4]/div[1]/div/div/div/div/div[7]/div[1]/div/div/div[1]/div/div/div/div/div/div/input')
name.send_keys("计算机网络")
time.sleep(3)
searchbutton = driver.find_element(By.CSS_SELECTOR, ".j-searchBtn")
searchbutton.click()
time.sleep(2)
con,cursor = openDB()
j = 0
html = driver.page_source
se = Selector(text=html)
lists = se.xpath('/html/body/div[4]/div[2]/div[2]/div[2]/div/div[6]/div[2]/div[1]/div/div/div')
for ls in lists:
    j += 1
    courname = "".join(ls.xpath('./div[2]/div/div/div[1]/a[1]//span/text()').extract())
    schoolname = ls.xpath('./div[2]/div/div/div[2]/a[1]/text()').extract_first()
    teachername = "".join(ls.xpath('./div[2]/div/div/div[2]/a[2]/text()').extract())
    teamname = "、".join(ls.xpath('./div[2]/div/div/div[2]/span//a/text()').extract())
    number = ls.xpath('./div[2]/div/div/div[3]/span[2]/text()').extract_first()
    process = ls.xpath('./div[2]/div/div/div[3]/div/span[2]/text()').extract_first()
    intro = "".join( ls.xpath('./div[2]/div/div/a/span/text()').extract())
    insert(cursor, str(j),courname,schoolname,teachername,teamname,number,process,intro)
    print(j,courname,schoolname,teachername,teamname,number,process,intro)
driver.close()
show(cursor)
closeDB(con)
  • 输出信息

  • Gitee文件夹链接
    题二

  • 心得体会
    对于有iframm的弹窗页面,需要转入该弹窗,不然无法定位该弹窗,也无法设置数据

作业③:

  • 要求:掌握大数据相关服务,熟悉 Xshell 的使用
    完成文档 华为云_大数据实时分析处理实验手册-Flume 日志采集实验(部
    分)v2.docx 中的任务。

  • 环境搭建:

    • 任务一:开通 MapReduce 服务
  • 实时分析开发实战:

    • 任务一:Python 脚本生成测试数据

    • 任务二:配置 Kafka

    • 任务三: 安装 Flume 客户端

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

  • 心得体会
    1.对于服务器,数据的安全十分重要,注意要使用较多的防护手段
    2.在对服务器flume,kafka进行配置时,要注意使用自己申请的ip,才能访问到数据。