首页 > 编程语言 > 详细

Python高级应用程序设计任务

时间:2019-12-15 15:09:31      阅读:75      评论:0      收藏:0      [点我收藏+]
一、主题式网络爬虫设计方案(15分)
1.主题式网络爬虫名称
刀塔2不朽品质饰品交易分析
2.主题式网络爬虫爬取的内容与数据特征分析
对不朽饰品的成交价格和时间爬取
3.主题式网络爬虫设计方案概述(包括实现思路与技术难点)
 用request库从商品列表获取单个商品的url,然后从商品获取名字和价格,然后根据价格做出图表已达到数据可视化持久化。
二、主题页面的结构特征分析(15分)
1.主题页面的结构特征
https://www.c5game.com/dota.html?rarity=immortal&page={}
通过填page值获取商品页面数据
https://www.c5game.com/dota/history/{}.
通过填{}获取不同商品的信息
2.Htmls页面解析
技术分享图片
从class类型为name的p标签下面的a标签的href中取商品的url值
 
技术分享图片
在class为name的div标签取的商品名字 为ft-gold的span标签取得价格
3.节点(标签)查找方法与遍历方法
(必要时画出节点树结构)
 我使用find_all()方法进行特定标签查找
三、网络爬虫程序设计(60分)
爬虫程序主体要包括以下各部分,要附源代码及较详细注释,并在每部分程序后面提供输出结果的截图。
fimport requests,  matplotlib, re
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup


def ahtml(url):
    a = {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36}
    try:
        data = requests.get(url, headers=a)
        data.raise_for_status()
        data.encoding = data.apparent_encoding
        return data.text
    except:
        return " "

#获取商品代码
def getcommodity(coms):
    for i in range(1, 3):
        html = ahtml(https://www.c5game.com/dota.html?rarity=immortal&page={}.format(i))
        #bs4格式化html页面
        soup = BeautifulSoup(html, "html.parser")
        #遍历span标签内a标签的字符串
        for span in soup.find_all("p", attrs="name"):
            #将字符串放进列表
            temp = span.a.attrs[href]
            #正则过滤href中的元素
            temp1 = temp.split(/)[2]
            temp2 = temp1.split(-)[0]
            coms.append(temp2)
    return coms

#待会遍历列表放mid进来
def getprice(coms):
    count = []
    html = ahtml(https://www.c5game.com/dota/history/{}.html.format(coms))
    #bs4格式化html页面
    soup = BeautifulSoup(html, "html.parser")
    #遍历span标签内a标签的字符串
    span = soup.find_all("span", attrs="ft-gold")
    i = 0
    while i < 8:
        # 将字符串放进列表
        temp = span[i].string
        #取¥后的值
        count.append(eval(temp.split()[1]))
        i += 1
    return count

def getyear(coms):
    count = []
    html = ahtml(https://www.c5game.com/dota/history/{}.html.format(coms))
    #bs4格式化html页面
    soup = BeautifulSoup(html, "html.parser")
    span = soup.find_all("td")
    i = 0
    while i < 8:
        try:
            num = re.findall(r"\d{2}[/-]\d{2}[/-]\d{2}", span.string)
            count.append(num)
            i += 1
        except:
            continue
    return count


def getname(coms):
    name = []
    html = ahtml(https://www.c5game.com/dota/history/{}.html.format(coms))
    #bs4格式化html页面
    soup = BeautifulSoup(html, "html.parser")
    span = soup.find_all("div", attrs="name-ellipsis")
    #将字符串放进列表
    name.append(span[0].string)
    return name


def ChartBroken(x, y, doc):
    plt.figure()
    plt.plot(x, y)
    #y轴
    plt.ylabel(Price)
    #x轴
    plt.xlabel(Times)
    #标题名
    plt.title(doc)
    #保存文件
    plt.savefig(doc, dpi=600)
    plt.show()

def ChartBar(x, y, doc):
    plt.figure()
    plt.bar(left=x, height=y, color=b, width=0.5)
    plt.ylabel(Price)
    plt.xlabel(Times)
    plt.title(doc)
    # 保存程序结果,数据持久化
    plt.savefig(doc, dpi=600)
    plt.show()


#修正pandas绘图中文乱码问题
matplotlib.rcParams[font.sans-serif] = [SimHei]
matplotlib.rcParams[font.family]=sans-serif
matplotlib.rcParams[axes.unicode_minus] = False

#主函数
def main():
    a = []
    comslist = getcommodity(a)[0:3]
    i = 0
    while i < 3:
        name = getname(comslist[i])
        print(name)
        price = getprice(comslist[i])
        print(price)
        ChartBroken([1, 2, 3, 4, 5, 6, 7, 8], price, name[0])
        ChartBar([1, 2, 3, 4, 5, 6, 7, 8], price, name[0])
        i += 1

if __name__ == __main__:
  main()

1.数据爬取与采集

def getcommodity(coms):
    for i in range(1, 3):
        html = ahtml(https://www.c5game.com/dota.html?rarity=immortal&page={}.format(i))
        #bs4格式化html页面
        soup = BeautifulSoup(html, "html.parser")
def getprice(coms):
    count = []
    html = ahtml(https://www.c5game.com/dota/history/{}.html.format(coms))
    #bs4格式化html页面
    soup = BeautifulSoup(html, "html.parser")

 


2.对数据进行清洗和处理
 for span in soup.find_all("p", attrs="name"):
            #将字符串放进列表
            temp = span.a.attrs[href]
            #正则过滤href中的元素
            temp1 = temp.split(/)[2]
            temp2 = temp1.split(-)[0]
            coms.append(temp2)
    return coms

 


3.数据分析与可视化
(例如:数据柱形图、直方图、散点图、盒图、分布图、)
技术分享图片
技术分享图片
技术分享图片
def ChartBroken(x, y, doc):
    plt.figure()
    plt.plot(x, y)
    #y轴
    plt.ylabel(Price)
    #x轴
    plt.xlabel(Times)
    #标题名
    plt.title(doc)
    #保存文件
    plt.savefig(doc, dpi=600)
    plt.show()

def ChartBar(x, y, doc):
    plt.figure()
    plt.bar(left=x, height=y, color=b, width=0.5)
    plt.ylabel(Price)
    plt.xlabel(Times)
    plt.title(doc)
    # 保存程序结果,数据持久化
    plt.savefig(doc, dpi=600)
    plt.show()

 

 4.数据持久化
def ChartBroken(x, y, doc):
    plt.figure()
    plt.plot(x, y)
    #y轴
    plt.ylabel(Price)
    #x轴
    plt.xlabel(Times)
    #标题名
    plt.title(doc)
    #保存文件
    plt.savefig(doc, dpi=600)
    plt.show()

def ChartBar(x, y, doc):
    plt.figure()
    plt.bar(left=x, height=y, color=b, width=0.5)
    plt.ylabel(Price)
    plt.xlabel(Times)
    plt.title(doc)
    # 保存程序结果,数据持久化
    plt.savefig(doc, dpi=600)
    plt.show()

 

 
四、结论(10分)
1.经过对主题数据的分析与可视化,可以得到哪些结论?
数据的可视化使得爬取的数据集更加清晰明显
对数据的分析使得自己操作的更熟练
2.对本次程序设计任务完成的情况做一个简单的小结。
基本像爬的数据都爬了,就是有一个商品成交时间的爬取有点麻烦,没有用上

Python高级应用程序设计任务

原文:https://www.cnblogs.com/gjz0417/p/12043983.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!