因为最近没事在学python,正好最近也想趴下新浪微博上边的一些数据,在这里主要爬去的是一我的的粉丝具体信息(微博昵称,我的介绍,地址,经过什么方式进行关注),因此就学以至用,经过python来爬去微博上边的数据。html
首先先说下环境啊,使用的是python3.5,而后使用的框架有:python
requests:用来获取html页面。json
BeautifulSoup:用来进行html的解析,是一个在python爬虫中很是好用的一个工具,而且有中文的说明文档,连接是:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html。能够看其中具体的一些函数的使用的方法。数组
经过这两个,则就能够实现咱们想要实现的功能了。cookie
而后第二步,则是咱们须要模拟微博进行登陆,由于你会发现,若是你不登陆,是没法看一我的的具体的粉丝信息的,所以咱们须要本身登陆下新浪微博,而后经过调试工具,把cookie复制出来,这样才可以进行爬虫。,怎么获取cookie,在这进行一个简单的介绍,登录后看到我的主页后,打开开发者工具,而后选择network:python爬虫
而后复制下这个cookie,在爬虫中须要用到,接下来就上代码了:框架
主程序类代码:dom
import requests from html.parser import HTMLParser import person from bs4 import BeautifulSoup import json #获取的cookie值存放在这 myHeader = {"Cookie":"SINAGLOBAL=1151648924265.729.1510207774298; YF-V5-G0=a9b587b1791ab233f24db4e09dad383c; login_sid_t=663888f6033b6f4a8f5fa48b26d9eb17; YF-Ugrow-G0=ea90f703b7694b74b62d38420b5273df; _s_tentry=passport.weibo.com; Apache=9283625770163.1.1512087277478; ULV=1512087277483:2:1:1:9283625770163.1.1512087277478:1510207774304; SSOLoginState=1512087292; wvr=6; YF-Page-G0=451b3eb7a5a4008f8b81de1fcc8cf90e; cross_origin_proto=SSL; WBStorage=82ca67f06fa80da0|undefined; crossidccode=CODE-gz-1ElEPq-16RrfZ-qpysbLqGTWJetzH095150; SCF=AnQFFpBKBne2YCQtu52G1zEuEpkY1WI_QdgCdIs-ANt1_wzGQ0_VgvzYW7PLnswMwwJgI9T3YeRDGsWhfOwoLBs.; SUB=_2A253IOm1DeThGeNG6lsU-CjOzTWIHXVUVFx9rDV8PUNbmtBeLWTSkW9NS2IjRFgpnHs1R3f_H3nB67BbC--9b_Hb; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5fUsSPaZjP3cB4EXR8M3gT5JpX5KzhUgL.Fo-ReK.f1hqESo.2dJLoIEXLxK.L1hzLBKeLxK-LBo.LBoBLxKML1-zL1-zLxK-LBKBL12qLxK-L1K-L122t; SUHB=0wnlry4ys0tunb; ALF=1543884132; wb_cusLike_5819586269=N; UOR=,,login.sina.com.cn"} #要爬去的帐号的粉丝列表页面的地址
r = requests.get('https://weibo.com/p/1005051678105910/follow?relate=fans&from=100505&wvr=6&mod=headfans¤t=fans#place',headers=myHeader) f = open("test.html", "w", encoding="UTF-8") parser = HTMLParser() parser.feed(r.text) htmlStr = r.text # 经过script来切割后边的几个经过js来显示的json数组,经过观看源代码 fansStr = htmlStr.split("</script>") #由于在测试的时候,发现微博每一次返回的dom的顺序不同,粉丝列表的dom和一个其余内容的dom的位置一直交替,因此在这加了一个判断 tmpJson = fansStr[-2][17:-1] if fansStr[-2][17:-1].__len__()>fansStr[-3][17:-1].__len__() else fansStr[-3][17:-1] dict = json.loads(tmpJson) soup = BeautifulSoup(dict['html'], 'html') soup.prettify() f.write(soup.prettify()) for divTag in soup.find_all('div'): if divTag['class'] == ["follow_inner"]: followTag = divTag if locals().get("followTag"): for personTag in followTag.find_all('dl'): p = person.person(personTag) print(p.__dict__)
person类代码:函数
在这中间进行主要的解析工具
from bs4 import BeautifulSoup
#具体解析在这 class person(object): def __init__(self, personTag = None): self.analysis(personTag) def analysis(self,personTag): self.analysisName(personTag) self.analysisFollowAndFansNumber(personTag) self.analysisCity(personTag) self.analysisIntroduce(personTag) self.analysisFollowWay(personTag) self.analysisID(personTag) def analysisName(self,personTag): self.name = personTag.div.a.string def analysisFollowAndFansNumber(self,personTag): for divTag in personTag.find_all('div'): if divTag['class'] == ["info_connect"]: infoTag = divTag if locals().get("infoTag"): self.followNumber = infoTag.find_all('span')[0].em.string self.fansNumber = infoTag.find_all('span')[1].em.a.string self.assay = infoTag.find_all('span')[2].em.a.string def analysisCity(self,personTag): for divTag in personTag.find_all('div'): if divTag['class'] == ['info_add']: addressTag = divTag if locals().get('addressTag'): self.address = addressTag.span.string def analysisIntroduce(self,personTag): for divTag in personTag.find_all('div'): if divTag['class'] == ['info_intro']: introduceTag = divTag if locals().get('introduceTag'): self.introduce = introduceTag.span.string def analysisFollowWay(self,personTag): for divTag in personTag.find_all('div'): if divTag['class'] == ['info_from']: fromTag = divTag if locals().get('fromTag'): self.fromInfo = fromTag.a.string def analysisID(self,personTag): personRel = personTag.dt.a['href'] self.id = personRel[personRel.find('=')+1:-5]+personRel[3:personRel.find('?')]
在这里爬去的是孙俪下边的第一页列表的微博的粉丝,结果以下截图:
其实这个相对仍是比较简单的,主要比较麻烦的是须要看新浪的html的源代码,须要了解其显示的规律,而后使用beautiful soup进行解析节点,获取数据。