使用python2提取firefox历史

2024-06-08 08:08:44 发布

您现在位置:Python中文网/ 问答频道 /正文

它将打印前20名firefox历史记录并保存到html中文件。它都会在Python3里完成。 我必须在Python2里做同样的事情。 有没有什么方法可以修改这个代码或者用别的方法来做python2。你知道吗

import platform
import os
import browserhistory as bh
def printTohtml(htmlfile):    
    html =  "<html>\n<head></head>\n<style>p { margin-bottom:5px  !important;  }</style>\n<body>\n"

    title = "Browser History"
    html += '\n<p style = "background-color: #92a8d1 !important; color:white !important; font-size:24px !important; text-align: center !important; letter-spacing: 5px !important; ">' + title + '</p>\n'


    para = '<p style = "background-color: #92a8d1 !important; color:white !important; font-size:24px !important; text-align: center !important; letter-spacing: 5px !important;">' + browserhistory1 + '</p>\n'
    html += para
    dict_obj = bh.get_browserhistory()
    for i in range(0 ,19):
        html += '<p style = "white-space: pre-line !important; line-height: 28px !important;">' + str(dict_obj['firefox'][i]) + '</p>\n'

    with open(htmlfile, 'w') as f:
        f.write(html + "\n</body>\n</html>")

browserhistory1 = ("Browser History")

printTohtml('firefox.html')

Tags: 方法importstylehtmlasbodyfirefoxhead
1条回答
网友
1楼 · 发布于 2024-06-08 08:08:44

下面的代码应该有效

import os
import sqlite3
import operator
from collections import OrderedDict
import matplotlib.pyplot as plt
from urlparse import urlparse

def parse(url):
try:
parse_url = urlparse(url)
domain = parse_url.netloc
return domain
except IndexError:
print("URL format error")

def analyse(results):

plt.bar(range(len(results)), results.values(), align='edge')
plt.xticks(rotation=20)
plt.xticks(range(len(results)), results.keys())

plt.show()

data_path = os.path.expanduser('~')+"/.mozilla/firefox/7xov879d.default"
files = os.listdir(data_path)
history_db = os.path.join(data_path, 'places.sqlite')

c = sqlite3.connect(history_db)
cursor = c.cursor()
select_statement = "select moz_places.url, moz_places.visit_count from moz_places;"
cursor.execute(select_statement)

results = cursor.fetchall()
sites_count = {}

for url, count in results:
url = parse(url)

if url in sites_count:
sites_count[url] += 1
else:
sites_count[url] = 1

sites_count_sorted = OrderedDict(sorted(sites_count.items(), key=operator.itemgetter(1), reverse=True)[:10])

analyse(sites_count_sorted)

相关问题 更多 >