要抓取http://www.alexa.cn/rank/baidu.com网站的排名信息:例如抓取以下信息:

  

   需要微信扫描登录

    因为这个网站抓取数据是收费,所以就利用网站提供API服务获取json信息:

  

  

  

  上面的API KEY值需要花钱买的(注意开通会员的方式不行,必须以10000次查询 49.00 元这种方式,比较坑爹啊)

  具体python代码

  

# coding=utf-8
import json
import httplib2
import json
import xlrd
import xlwt
import os
import datetime
import time
class alexa: def __init__(self,key="7Z4ddd6ywaQuo6RkKfI3SzGeKn8Mavde"):
self.key = key def WriteLog(self, message,date):
fileName = os.path.join(os.getcwd(), 'alexa/' + date + '.txt')
with open(fileName, 'a') as f:
f.write(message) def WriteSheetRow(self,sheet, rowValueList, rowIndex, isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
# style = xlwt.easyxf('font: bold 0, color red;')#红色字体
style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 设置Excel单元格的背景色为黄色,字体为粗体
for svalue in rowValueList:
if isBold:
sheet.write(rowIndex, i, svalue, style2)
else:
sheet.write(rowIndex, i, svalue)
i = i + 1 def save_Excel(self,headList,valuelist,fileName):
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1', cell_overwrite_ok=True)
# headList = ['周期', '全球网站排名', '变化趋势', '日均UV']
rowIndex = 0
self.WriteSheetRow(sheet, headList, rowIndex, True)
for lst in valuelist:
rowIndex+=1
self.WriteSheetRow(sheet, lst, rowIndex, False)
wbk.save(fileName) def getAlexaData(self,domain):
url="http://api.alexa.cn/alexa/details?site=%s&key=%s"%(domain,self.key)
try:
h = httplib2.Http(".cache")
(resp_headers, content) = h.request(url, "GET")
data = json.loads(content.decode('utf8'))
self.parserData(data)
# print(data) except Exception as e1:
error = "ex" def parserData(self,data):
# f = open("alexa.txt", "r")
# txt = f.read()
# data = json.loads(txt) traffic_dict = data["result"]["traffic_data"]
day = traffic_dict["day"] week = traffic_dict["week"]
month = traffic_dict["month"]
three_month = traffic_dict["three_month"] trafic_headList = ['周期', '全球网站排名', '变化趋势', '日均UV', '日均PV']
traffic_data_list =[]
day_list = ["当日"]
week_list = ["周平均"]
month_list = ["月平均"]
three_month_list = ["三月平均"]
trafic = ["time_range", "traffic_rank", "traffic_rank_delta", "avg_daily_uv", "avg_daily_pv"]
length = len(trafic)
for i in range(1,length):
day_list.append(day[trafic[i]])
week_list.append(week[trafic[i]])
month_list.append(month[trafic[i]])
three_month_list.append(three_month[trafic[i]]) traffic_data_list.append(day_list)
traffic_data_list.append(week_list)
traffic_data_list.append(month_list)
traffic_data_list.append(three_month_list) fileName = datetime.datetime.now().strftime('%Y-%m-%d')+"_traffic.xlsx"
fileName = os.path.join(os.getcwd(),fileName)
self.save_Excel(trafic_headList,traffic_data_list,fileName) country_headList = ['国家/地区名称', '国家/地区代码', '国家/地区排名', '网站访问比例', '页面浏览比例']
country_data_list = []
country_data = data["result"]["country_data"]
col_list = ["country","code","rank","per_users","per_pageviews"]
length = len(col_list)
for item in country_data:
lst =[]
for i in range(0,length):
lst.append(item[col_list[i]])
country_data_list.append(lst) fileName = datetime.datetime.now().strftime('%Y-%m-%d') + "_country.xlsx"
fileName = os.path.join(os.getcwd(), fileName)
self.save_Excel(country_headList, country_data_list, fileName) subdomains_headList = ['被访问网址', '近月网站访问比例', '近月页面访问比例', '人均页面浏览量']
subdomains_data_list = []
subdomains_data = data["result"]["subdomains_data"]
sub_col_list = ["subdomain", "reach_percentage", "pageviews_percentage", "pageviews_peruser"]
length = len(sub_col_list)
for item in subdomains_data:
lst = []
for i in range(0, length):
lst.append(item[sub_col_list[i]])
subdomains_data_list.append(lst) fileName = datetime.datetime.now().strftime('%Y-%m-%d') + "_subdomains.xlsx"
fileName = os.path.join(os.getcwd(), fileName)
self.save_Excel(subdomains_headList, subdomains_data_list, fileName) # print(("%s,%s,%s,%s,%s") % (day[trafic[0]], day[trafic[1]], day[trafic[2]], day[trafic[3]], day[trafic[4]]))
# print(("%s,%s,%s,%s,%s") % (week[trafic[0]], week[trafic[1]], week[trafic[2]], week[trafic[3]], week[trafic[4]]))
# print(("%s,%s,%s,%s,%s") % (month[trafic[0]], month[trafic[1]], month[trafic[2]], month[trafic[3]], month[trafic[4]]))
# print(("%s,%s,%s,%s,%s") % (three_month[trafic[0]], three_month[trafic[1]], three_month[trafic[2]], three_month[trafic[3]], three_month[trafic[4]]))
# print("\n") # print("country_data")
# country_data = data["result"]["country_data"]
# for item in country_data:
# print(("%s,%s,%s,%s,%s") % (item["country"], item["code"], item["rank"], item["per_users"], item["per_pageviews"]))
#
# print("\n")
# print("subdomains_data")
# subdomains_data = data["result"]["subdomains_data"]
# for item in subdomains_data:
# print(("%s,%s,%s,%s") % (item["subdomain"], item["reach_percentage"], item["pageviews_percentage"], item["pageviews_peruser"])) obj = alexa()
obj.getAlexaData("baidu.com")
# obj.parserData("")

  

python 抓取alexa数据的更多相关文章

  1. python 抓取金融数据,pandas进行数据分析并可视化系列 (一)

    终于盼来了不是前言部分的前言,相当于杂谈,算得上闲扯,我觉得很多东西都是在闲扯中感悟的,比如需求这东西,一个人只有跟自己沟通好了,总结出某些东西了,才能更好的和别人去聊,去说. 今天这篇写的是明白需求 ...

  2. 利用python抓取页面数据

    1.首先是安装python(注意python3.X和python2.X是不兼容的,我们最好用python3.X) 安装方法:安装python 2.安装成功后,再进行我们需要的插件安装.(这里我们需要用 ...

  3. 记录使用jQuery和Python抓取采集数据的一个实例

    从现成的网站上抓取汽车品牌,型号,车系的数据库记录. 先看成果,大概4w条车款记录 一共建了四张表,分别存储品牌,车系,车型和车款 大概过程: 使用jQuery获取页面中呈现的大批内容 能通过页面一次 ...

  4. 使用python抓取App数据

    App接口爬取数据过程使用抓包工具手机使用代理,app所有请求通过抓包工具获得接口,分析接口反编译apk获取key突破反爬限制需要的工具:夜神模拟器FiddlerPycharm实现过程首先下载夜神模拟 ...

  5. 网络爬虫-使用Python抓取网页数据

    搬自大神boyXiong的干货! 闲来无事,看看了Python,发现这东西挺爽的,废话少说,就是干 准备搭建环境 因为是MAC电脑,所以自动安装了Python 2.7的版本 添加一个 库 Beauti ...

  6. Python抓取双色球数据

    数据来源网站http://baidu.lecai.com/lottery/draw/list/50?d=2013-01-01 HTML解析器http://pythonhosted.org/pyquer ...

  7. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  8. 如何用python抓取js生成的数据 - SegmentFault

    如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...

  9. Python抓取百度百科数据

    前言 本文整理自慕课网<Python开发简单爬虫>,将会记录爬取百度百科"python"词条相关页面的整个过程. 抓取策略 确定目标:确定抓取哪个网站的哪些页面的哪部分 ...

随机推荐

  1. 【贪心】Codeforces Round #480 (Div. 2) C. Posterized

    题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...

  2. 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken

    题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ...

  3. 20162327WJH使用队列:模拟票务站台代码分析

    20162327WJH使用队列:模拟票务站台代码分析 用链队实现队列的情况 1.用链表实现队列的代码 关键方法代码及补全代(LinkedOueue类) public void enqueue(T el ...

  4. Spring Boot 运作原理

    Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...

  5. 解耦你的HTML,CSS和JAVASRIPT

    注:本文为翻译文章,原文<Decoupling Your HTML, CSS, and JavaScript> 今天在web上任何大一点的网站或应用程序都包含大量的html,css和jav ...

  6. Ajax 的概念及过程?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?

    Ajax 是什么: 1)  通过异步模式,提升了用户体验 2)  优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用 3)  Ajax 在客户端运行,承担了一部分本来由服务器承担的工 ...

  7. PIXIV 爬取国际前100名代码

    PYTHON爬虫 爬取PIXIV国际前100名的代码 代码是别人的,那天学习爬虫的时候看到了,写的很厉害~ 学习学习 #coding:UTF-8 __author__ = 'monburan' __v ...

  8. FolderSync :The various features and how to use them

    Help - Tacit Dynamics Help This page describes the various features of FolderSync and how to use the ...

  9. Detecting Underlying Linux Distro

    If you are the owner of the system, then you know which Linux is installed and running. This article ...

  10. 2008 SCI 影响因子(Impact Factor)

    2008 SCI 影响因子(Impact Factor) Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW E ...