Habrastatistics:探索网站访问量最大和最少的部分

你好,哈布尔。

В 上一节 Habr 的流量是根据主要参数(文章数量、浏览量和评级)进行分析的。 然而,网站部分的受欢迎程度问题仍未得到检验。 更详细地研究这个问题并找到最受欢迎和最不受欢迎的中心变得很有趣。 最后,我将更详细地了解极客时代效应,最后根据新排名重新选择最佳文章。

Habrastatistics:探索网站访问量最大和最少的部分

对于那些对发生的事情感兴趣的人来说,续集已被削减。

让我再次提醒您,统计数据和评级不是官方的,我没有任何内幕消息。 也不能保证我没有在某个地方犯错误或遗漏什么。 但我仍然认为这很有趣。 我们首先从代码开始;对此不感兴趣的人可以跳过第一部分。

数据收集

在解析器的第一个版本中,仅考虑视图、评论和文章评分的数量。 这已经很好了,但是它不允许您进行更复杂的查询。 是时候分析网站的主题部分了;这将使您能够进行非常有趣的研究,例如,了解“C++”部分的受欢迎程度在过去几年中发生了怎样的变化。

文章解析器得到了改进,现在它返回文章所属的中心,以及作者的昵称和他的评分(这里也可以做很多有趣的事情,但这将在稍后进行)。 数据保存在 csv 文件中,如下所示:

2018-12-18T12:43Z,https://habr.com/ru/post/433550/,"Мессенджер Slack — причины выбора, косяки при внедрении и особенности сервиса, облегчающие жизнь",votes:7,votesplus:8,votesmin:1,bookmarks:32,
views:8300,comments:10,user:ReDisque,karma:5,subscribers:2,hubs:productpm+soft
...

我们将收到该网站主要主题中心的列表。

def get_as_str(link: str) -> Str:
    try:
        r = requests.get(link)
        return Str(r.text)
    except Exception as e:
        return Str("")

def get_hubs():
    hubs = []
    for p in range(1, 12):
        page_html = get_as_str("https://habr.com/ru/hubs/page%d/" % p)
        # page_html = get_as_str("https://habr.com/ru/hubs/geektimes/page%d/" % p)  # Geektimes
        # page_html = get_as_str("https://habr.com/ru/hubs/develop/page%d/" % p)  # Develop
        # page_html = get_as_str("https://habr.com/ru/hubs/admin/page%d" % p)  # Admin
        for hub in page_html.split("media-obj media-obj_hub"):
            info = Str(hub).find_between('"https://habr.com/ru/hub', 'list-snippet__tags') 
            if "*</span>" in info:
                hub_name = info.find_between('/', '/"')
                if len(hub_name) > 0 and len(hub_name) < 32:
                    hubs.append(hub_name)
    print(hubs)

find_ Between 函数和 Str 类选择两个标签之间的字符串,我使用了它们 早期。 主题中心标有“*”,因此可以轻松突出显示它们,您还可以取消注释相应的行以获取其他类别的部分。

get_hubs 函数的输出是一个相当令人印象深刻的列表,我们将其保存为字典。 我专门完整地展示了该列表,以便您可以估计其数量。

hubs_profile = {'infosecurity', 'programming', 'webdev', 'python', 'sys_admin', 'it-infrastructure', 'devops', 'javascript', 'open_source', 'network_technologies', 'gamedev', 'cpp', 'machine_learning', 'pm', 'hr_management', 'linux', 'analysis_design', 'ui', 'net', 'hi', 'maths', 'mobile_dev', 'productpm', 'win_dev', 'it_testing', 'dev_management', 'algorithms', 'go', 'php', 'csharp', 'nix', 'data_visualization', 'web_testing', 's_admin', 'crazydev', 'data_mining', 'bigdata', 'c', 'java', 'usability', 'instant_messaging', 'gtd', 'system_programming', 'ios_dev', 'oop', 'nginx', 'kubernetes', 'sql', '3d_graphics', 'css', 'geo', 'image_processing', 'controllers', 'game_design', 'html5', 'community_management', 'electronics', 'android_dev', 'crypto', 'netdev', 'cisconetworks', 'db_admins', 'funcprog', 'wireless', 'dwh', 'linux_dev', 'assembler', 'reactjs', 'sales', 'microservices', 'search_technologies', 'compilers', 'virtualization', 'client_side_optimization', 'distributed_systems', 'api', 'media_management', 'complete_code', 'typescript', 'postgresql', 'rust', 'agile', 'refactoring', 'parallel_programming', 'mssql', 'game_promotion', 'robo_dev', 'reverse-engineering', 'web_analytics', 'unity', 'symfony', 'build_automation', 'swift', 'raspberrypi', 'web_design', 'kotlin', 'debug', 'pay_system', 'apps_design', 'git', 'shells', 'laravel', 'mobile_testing', 'openstreetmap', 'lua', 'vs', 'yii', 'sport_programming', 'service_desk', 'itstandarts', 'nodejs', 'data_warehouse', 'ctf', 'erp', 'video', 'mobileanalytics', 'ipv6', 'virus', 'crm', 'backup', 'mesh_networking', 'cad_cam', 'patents', 'cloud_computing', 'growthhacking', 'iot_dev', 'server_side_optimization', 'latex', 'natural_language_processing', 'scala', 'unreal_engine', 'mongodb', 'delphi',  'industrial_control_system', 'r', 'fpga', 'oracle', 'arduino', 'magento', 'ruby', 'nosql', 'flutter', 'xml', 'apache', 'sveltejs', 'devmail', 'ecommerce_development', 'opendata', 'Hadoop', 'yandex_api', 'game_monetization', 'ror', 'graph_design', 'scada', 'mobile_monetization', 'sqlite', 'accessibility', 'saas', 'helpdesk', 'matlab', 'julia', 'aws', 'data_recovery', 'erlang', 'angular', 'osx_dev', 'dns', 'dart', 'vector_graphics', 'asp', 'domains', 'cvs', 'asterisk', 'iis', 'it_monetization', 'localization', 'objectivec', 'IPFS', 'jquery', 'lisp', 'arvrdev', 'powershell', 'd', 'conversion', 'animation', 'webgl', 'wordpress', 'elm', 'qt_software', 'google_api', 'groovy_grails', 'Sailfish_dev', 'Atlassian', 'desktop_environment', 'game_testing', 'mysql', 'ecm', 'cms', 'Xamarin', 'haskell', 'prototyping', 'sw', 'django', 'gradle', 'billing', 'tdd', 'openshift', 'canvas', 'map_api', 'vuejs', 'data_compression', 'tizen_dev', 'iptv', 'mono', 'labview', 'perl', 'AJAX', 'ms_access', 'gpgpu', 'infolust', 'microformats', 'facebook_api', 'vba', 'twitter_api', 'twisted', 'phalcon', 'joomla', 'action_script', 'flex', 'gtk', 'meteorjs', 'iconoskaz', 'cobol', 'cocoa', 'fortran', 'uml', 'codeigniter', 'prolog', 'mercurial', 'drupal', 'wp_dev', 'smallbasic', 'webassembly', 'cubrid', 'fido', 'bada_dev', 'cgi', 'extjs', 'zend_framework', 'typography', 'UEFI', 'geo_systems', 'vim', 'creative_commons', 'modx', 'derbyjs', 'xcode', 'greasemonkey', 'i2p', 'flash_platform', 'coffeescript', 'fsharp', 'clojure', 'puppet', 'forth', 'processing_lang', 'firebird', 'javame_dev', 'cakephp', 'google_cloud_vision_api', 'kohanaphp', 'elixirphoenix', 'eclipse', 'xslt', 'smalltalk', 'googlecloud', 'gae', 'mootools', 'emacs', 'flask', 'gwt', 'web_monetization', 'circuit-design', 'office365dev', 'haxe', 'doctrine', 'typo3', 'regex', 'solidity', 'brainfuck', 'sphinx', 'san', 'vk_api', 'ecommerce'}

相比之下,极客时代部分看起来更朴素:

hubs_gt = {'popular_science', 'history', 'soft', 'lifehacks', 'health', 'finance', 'artificial_intelligence', 'itcompanies', 'DIY', 'energy', 'transport', 'gadgets', 'social_networks', 'space', 'futurenow', 'it_bigraphy', 'antikvariat', 'games', 'hardware', 'learning_languages', 'urban', 'brain', 'internet_of_things', 'easyelectronics', 'cellular', 'physics', 'cryptocurrency', 'interviews', 'biotech', 'network_hardware', 'autogadgets', 'lasers', 'sound', 'home_automation', 'smartphones', 'statistics', 'robot', 'cpu', 'video_tech', 'Ecology', 'presentation', 'desktops', 'wearable_electronics', 'quantum', 'notebooks', 'cyberpunk', 'Peripheral', 'demoscene', 'copyright', 'astronomy', 'arvr', 'medgadgets', '3d-printers', 'Chemistry', 'storages', 'sci-fi', 'logic_games', 'office', 'tablets', 'displays', 'video_conferencing', 'videocards', 'photo', 'multicopters', 'supercomputers', 'telemedicine', 'cybersport', 'nano', 'crowdsourcing', 'infographics'}

其余的枢纽也以同样的方式保存。 现在很容易编写一个函数来返回结果,无论该文章属于 geektimes 还是个人资料中心。

def is_geektimes(hubs: List) -> bool:
    return len(set(hubs) & hubs_gt) > 0

def is_geektimes_only(hubs: List) -> bool:
    return is_geektimes(hubs) is True and is_profile(hubs) is False

def is_profile(hubs: List) -> bool:
    return len(set(hubs) & hubs_profile) > 0

其他部分(“开发”、“管理”等)也有类似的功能。

处理

是时候开始分析了。 我们加载数据集并处理中心数据。

def to_list(s: str) -> List[str]:
    # "user:popular_science+astronomy" => [popular_science, astronomy]
    return s.split(':')[1].split('+')

def to_date(dt: datetime) -> datetime.date:
    return dt.date()

df = pd.read_csv("habr_2019.csv", sep=',', encoding='utf-8', error_bad_lines=True, quotechar='"', comment='#')
dates = pd.to_datetime(df['datetime'], format='%Y-%m-%dT%H:%MZ')
dates += datetime.timedelta(hours=3)
df['date'] = dates.map(to_date, na_action=None)
hubs = df["hubs"].map(to_list, na_action=None)
df['hubs'] = hubs
df['is_profile'] = hubs.map(is_profile, na_action=None)
df['is_geektimes'] = hubs.map(is_geektimes, na_action=None)
df['is_geektimes_only'] = hubs.map(is_geektimes_only, na_action=None)
df['is_admin'] = hubs.map(is_admin, na_action=None)
df['is_develop'] = hubs.map(is_develop, na_action=None)

现在我们可以按天对数据进行分组,并显示不同中心的出版物数量。

g = df.groupby(['date'])
days_count = g.size().reset_index(name='counts')
year_days = days_count['date'].values
grouped = g.sum().reset_index()
profile_per_day_avg = grouped['is_profile'].rolling(window=20, min_periods=1).mean()
geektimes_per_day_avg = grouped['is_geektimes'].rolling(window=20, min_periods=1).mean()
geektimesonly_per_day_avg = grouped['is_geektimes_only'].rolling(window=20, min_periods=1).mean()
admin_per_day_avg = grouped['is_admin'].rolling(window=20, min_periods=1).mean()
develop_per_day_avg = grouped['is_develop'].rolling(window=20, min_periods=1).mean()

我们使用 Matplotlib 显示已发表文章的数量:

Habrastatistics:探索网站访问量最大和最少的部分

我在图表中将文章“geektimes”和“geektimes only”分开,因为一篇文章可以同时属于两个部分(例如,“DIY”+“微控制器”+“C++”)。 我使用“个人资料”这个名称来突出显示网站上的个人资料文章,尽管英语术语“个人资料”可能并不完全正确。

在上一部分中,我们询问了与今年夏天开始的极客时代文章付费规则变化相关的“极客时代效应”。 我们分别展示一下极客时代的文章:

df_gt = df[(df['is_geektimes_only'] == True)]
group_gt = df_gt.groupby(['date'])
days_count_gt = group_gt.size().reset_index(name='counts')
grouped = group_gt.sum().reset_index()
year_days_gt = days_count_gt['date'].values
view_gt_per_day_avg = grouped['views'].rolling(window=20, min_periods=1).mean()

结果很有趣。 极客时代文章的浏览量与总浏览量的比例大约在 1:5 左右。 不过,虽然总浏览量波动较大,但“娱乐”类文章的浏览量基本保持在同一水平。

Habrastatistics:探索网站访问量最大和最少的部分

您还可以注意到,“极客时代”版块的文章总浏览量在更改规则后仍然有所下降,但“肉眼可见”,下降幅度不超过总值的 5%。

查看每篇文章的平均浏览量很有趣:

Habrastatistics:探索网站访问量最大和最少的部分

对于“娱乐”文章,该比例比平均水平高出 40% 左右。 这可能并不奇怪。 四月初的失败对我来说并不清楚,也许这就是发生的事情,或者是某种解析错误,或者可能是极客时代的一位作者去度假了;)。

顺便说一句,该图显示了文章浏览量的两个更明显的峰值——元旦和五一假期。

集线器

让我们继续进行承诺的集线器分析。 让我们按观看次数列出前 20 个中心:

hubs_info = []
for hub_name in hubs_all:
    mask = df['hubs'].apply(lambda x: hub_name in x)
    df_hub = df[mask]

    count, views = df_hub.shape[0], df_hub['views'].sum()
    hubs_info.append((hub_name, count, views))

# Draw hubs
hubs_top = sorted(hubs_info, key=lambda v: v[2], reverse=True)[:20]
top_views = list(map(lambda x: x[2], hubs_top))
top_names = list(map(lambda x: x[0], hubs_top))

plt.rcParams["figure.figsize"] = (8, 6)
plt.bar(range(0, len(top_views)), top_views)
plt.xticks(range(0, len(top_names)), top_names, rotation=90)
plt.ticklabel_format(style='plain', axis='y')
plt.tight_layout()
plt.show()

结果:

Habrastatistics:探索网站访问量最大和最少的部分

令人惊讶的是,最受欢迎的主题是“信息安全”;排名前五的还包括“编程”和“科普”。

Antitop占据了Gtk和Cocoa。

Habrastatistics:探索网站访问量最大和最少的部分

告诉你一个秘密,顶级轮毂也能看到 这里,尽管那里没有显示观看次数。

等级

最后是承诺的评级。 使用中心分析数据,我们可以显示 2019 年最受欢迎的中心的最受欢迎文章。

信息安全

编程

科普

事业

信息技术立法

Web开发

GTK

最后,为了不冒犯任何人,我将给出访问量最少的中心“gtk”的评级。 一年之内就出版了 这篇文章也“自动”占据了评分的第一行。

结论

不会有任何结论。 大家阅读愉快。

来源: habr.com

添加评论