Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/x9/EQ/c-/-cE9Qxci8nTNwcuoC8IPWXuQlTGSxuOADzMMhiO2UAAdP5JaDArIQ48xMTQBnGcd2q4uq12wnxzfKEYdjnGa_XC-ofxhjuc-_brCAftPT_w.jpg)
项目背景
大家对于网页的数据爬虫了解的已经很多了,这次爬取APP端的数据。之前我也讲解过APP爬虫,但是没有讲解过Fiddler的配置和使用。这次以微博榜单为案例,介绍APP爬虫流程和数据的可视化(数据为2019年2月28号采集)。整个项目流程如下图所示:
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UACUwVMJA6zd49RhCPlJCo8Tui1fMezP23jDx3IMzGpQlMjlNrFcEVhBzFExUvgGPMw.jpg)
环境配置
首先,在网上下载Fiddler软件,这个和普通的软件安装一样,这里就不详细讲解了。
接着,我们打开Fiddler软件,进行配置,打开tools-options,进行如下的设置。
- 选中"Decrpt HTTPS traffic", Fiddler就可以截获HTTPS请求;
- 选中"Allow remote computers to connect". 是允许别的机器把HTTP/HTTPS请求发送到Fiddler上来。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UACdV1KT93FakIxKCUfs_em8QKg40rfn94gR_d2qF7LVcis3LiSmURqDIr9RpqhYyf4.jpg)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UAAT4N6bXZanvJ9jr5EiDUbM3BC6R7wGOqoZE-R22H8tuFS3nVDSq5GvJdEuHcTST_A.jpg)
设置完记得重启软件~
为了让Fiddler截取手机的包,需要让手机和电脑在同一个ip下,在电脑端的cmd中输入:ipconfig,获取电脑ip地址。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UADVnUpUsPiKwijYDDDVsLfy06sVOy3z25RHgbIlIS0sdRQS-FihXhLStU9uOcLehRg.jpg)
接着打开手机(以IPhone为例),打开wifi,配置代理,这里的代理就是上图我们查询的ip地址,8888为端口号。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UACUka4eZHa5LkpI-25bST1FVCVJdvlA_kCGt2G8U2AN_oDihucom4czl_bKp5-1dh8.jpg)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UADxzvs2aroZz_lEIwSugONehdC2a2oVCp4uGnyxUEgT92pu16cRcoRo0svRE2bvubE.jpg)
最后,在手机浏览器输入http://192.168.0.144:8888,点击下载证书,IPhone最后还需要在设置中信任证书,这样就可以用Fiddler抓取手机的包啦。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UAB_ziK4M12BSCXq-vMT-IZZw71dCJf8x9reIKuFJlOOhDhoXsfQMCclynxKy8yXxu0.jpg)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UADE9nx2Q1j2DvNjivGZ2PolA5MNERB2-uJaqyE38y9gXhHvN4wi6ZuhFwGxHuxDdI8.jpg)
找包
我们打开Fiddler找包,发现这是一个POST请求,下面就是请求的参数,max_id改变就是换页。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UADRORVV8dYId3NvIFMEcAgbhpMGpxYpDoyX6CBO6CSlAxd0NYjgdqgTZyOQvLikVg4.jpg)
爬虫代码
这样,我们就可以写出完整代码了。
import requests
import json
import csv
import time
fp = open('weibo.csv','w',newline='',encoding='utf-8')
writer = csv.writer(fp)
writer.writerow(['user','location','gender','followers','text','created_time','comments','good'])
url = 'https://api.weibo.cn/2/statuses/unread_hot_timeline?gsid=_2A25xXl6mDeRxGeNM41oV-S_MzDSIHXVQStVurDV6PUJbkdANLVTwkWpNSf8_0kSaOWZtWP9k_mbYuGWoJ6k7Z2E_&sensors_mark=0&wm=3333_2001&i=b9f7194&sensors_is_first_day=false&from=1092093010&b=0&c=iphone&networktype=wifi&skin=default&v_p=71&s=a8064444&v_f=1&sensors_device_id=1962A632-B40D-4337-A4C6-3F9AB979B517&lang=zh_CN&sflag=1&ua=iPhone8,1__weibo__9.2.0__iphone__os12.1.4&ft=11&aid=01AuxGxLabPA7Vzz8ZXBUpkeJqWbJ1woycR3lFBdLhoxgQC1I.&cum=21171544'
headers = {
'Host': 'api.weibo.cn',
'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
'User-Agent': 'Weibo/29278 (iPhone; iOS 11.4.1; Scale/2.00)'
}
params = ['max_id={}'.format(str(i)) for i in range(1,16)]
for param in params:
res = requests.post(url,headers=headers,data=param)
json_data = json.loads(res.text)
statuses = json_data['statuses']
for statuse in statuses:
user = statuse['user']['name']
location = statuse['user']['location']
gender = statuse['user']['gender']
followers = statuse['user']['followers_count']
text = statuse['text']
created_time = statuse['created_at']
comments = statuse['comments_count']
good = statuse['attitudes_count']
print(user,location,gender,followers,text,created_time,comments,good)
writer.writerow([user,location,gender,followers,text,created_time,comments,good])
time.sleep(2)
榜单分析
上榜明星,通过对用户昵称计数,看看哪些用户上榜最多。上榜的大多是明星和娱乐号主。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UADRwXL_NHTUc8qTTLddVzRuoKpHnO_8Qt0xT4M4sun2xB1k4P4VjAG0YsKPYhZSbXg.jpg)
地区分布,上榜的地区分布,可以看出大部分都来自北上广,然后海外用户也是有很多的(有些明星是外籍)。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/x9/EQ/c-/-cE9Qxci8nTNwcuoC8IPWXuQlTGSxuOADzMMhiO2UAA5S7KV9t6n0iQ6MvmVrGBFczmA1exGCrQ00jxO73p9LBMECma9o1g9Ouy5zrjs7do.jpg)
地区分布情况.png
性别分布,男性和女性分布较平均,男性更多一些~
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UADMzY9jSUHocxpqxLMprLkw_FStNz8JDKdTAoWKu3G0X5niernSVXpgIlbJjwLA65c.jpg)
榜单发布时间和词云图,可以看出,晚上8点是个高峰期,这个点刚好下班吃瓜。
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UAC2GPIeotg2Pl0jo4-B3wVu1mpCeMgxvCQvNPE4yedLMuiHO_fuoajdp_Jp3NOj1hc.jpg)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/ez/Ju/_m/m_JzueHnNG9dtZ-kWJtDtXuQlTGSxuOADzMMhiO2UAACICfojubDo06AdltmiBUBuE-OVV_aA6vCL5s3td5VxdBEN0eXjx1jEvWyF3QQ13Y.jpg)
下面是为初学者们准备的python电子书籍资料和python入门教程!
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UACZt6ySmTGueQSAGmfzqH8tmPUTKTMFz9hiew-U4mQ8PDIwlSIsUzVuDDi4O-eIcVs.jpg)
![Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程) Python有趣|微博榜单爬虫,尽知天下事(内附python学习教程)](https://cdn.ancii.com/article/image/v1/L3/7f/q4/4q73fLUeH6vHx4UXmTexBnuQlTGSxuOADzMMhiO2UABI08tgjiKCyLaZvT9z_68neZEBLLJToD-0Ilsnf1l1XW48m0VCAlUKS12EpEgvJyo.jpg)