#browser = webdriver.Chrome() #browser.add_cookie(cookie) #判断broser是否可用 if browser == None:
return
browser.get(url)
browser.save_screenshot(“f:****\\****test1.png”)
js = “var q=document.body.scrollTop=100000” #用于下滑页面的js命令
num = 0
#判断翻页按钮是否出现,如果没有出现,将鼠标下滑
while not is_ele_exist(browser,“W_pages”):
num+=1
browser.execute_script(js)
print(“还未找到元素”)
#最多翻页十次,防止进入死循环
if(num>10):
break
print(“用户的一页信息已加载完毕”)
html = browser.page_source
return html
‘’’
def has_source(browser,url):
判断一个微博页面是否有微博,如果没有,返回Fasle, 最开始考虑不周到,这个函数导致需要再次用webdriver打开页面 严重拖慢爬取速度,弃用
browser.get(url) if is_ele_exist(browser,“WB_feed_handle”): return True else: return False
‘’’
def has_source(html):
‘’’
判断一个微博页面是否有微博,如果没有,返回Fasle, 改良版,不用再次加载页面 ‘’’ bs = bs4.BeautifulSoup(html,“lxml”)
if bs.find(“div”,class_=“WB_detail”) !=None:
return True
else:
return False
def getusermessage(html):
‘’’
获取用户信息,包括头像,姓名。粉丝 :param html: :return:
‘’’ user = {}
bs = bs4.BeautifulSoup(html,“lxml”)
user[“姓名”] = bs.find_all(“h1”,class_=“username”)[0].text
user[“头像”] = bs.find_all(“p”,class_=“photo_wrap”)[0].find_all(“img”)[0][“src”]
user[“认证”] = bs.find_all(“div”,class_=“pf_intro”)[0][“title”]
messagebox = bs.find_all(“div”,class_=“WB_innerwrap”)
messages = messagebox[0].find_all(“td”,class_=“S_line1”)
for message in messages:
title = message.find_all(“span”,class_=“S_txt2”)[0].text
content = message.find_all(“strong”,class_=“W_f16”)[0].text
user[title] = content
print(user)
return user
def getweibo(filename,html):
‘’’
获取一个页面上的所有信息,包括每一条微博的内容,转发,评论,点赞数量 ‘’’ file = open(filename,“a”,encoding=“utf-8”)
beautifulsoup = bs4.BeautifulSoup(html,“lxml”)
weibos = beautifulsoup.find_all(“div”,class_="WB_cardwrap WB_feed_type S_bg2 ") #获得用于显示微博的父节点
weibonum = 0 #计算该页微博数量
for weibo in weibos:
#对每一个微博父节点进行处理
weibotext = weibo.find_all(“div”,class_ = “WB_text W_f14”) #获取微博文本内容
message = weibotext[0].text.replace(" ",“”) #取出文本中的所有空格
weibonum+=1 #微博数量+1
file.write(“\n第”+str(weibonum)+“条微博内容是:”+message+“\n”)
weibopics = weibo.find_all(“div”,class_=“WB_media_wrap clearfix”) #获取用于显示图片的标签
if len(weibopics) != 0: #判断标签是否存在
pics = weibopics[0].find_all(“img”) #获取该标签中的所有图片
for pic in pics:
file.write(pic[“src”]+“\n”)
weibomessages = weibo.find_all(“div”, class_=“WB_feed_handle”) # 获取微博信息父节点
weibomessagecontents = weibomessages[0].find_all(“a”, class_=“S_txt2”) #获取用于显示微博信息的节点(包括转发,评论,点赞数量)
for weibomessagecontent in weibomessagecontents[1:]: #分别获取信息
contents = weibomessagecontent.find_all(“em”)
file.write(contents[1].text+" ")
file.close()
print(“成功存储一个页面的微博”)
return weibonum
def getpageurl(url,num):
‘’’
根据一个用户的第一页获取后续的url http://www.weibo.com/u/1858002662?c=spr_sinamkt_buy_hyww_weibo_p113&is_hot=1#1496336038914 http://www.weibo.com/u/1858002662?is_search=0&visible=0&is_hot=1&is_tag=0&profile_ftype=1&page=2#feedtop
http://weibo.com/leehom?refer_flag=1005055013_&is_all=1 http://weibo.com/leehom?is_search=0&visible=0&is_all=1&is_tag=0&profile_ftype=1&page=2#feedtop 通过分析指导,page应该是表示的页面,is_all和is_hot应该对应的是热门微博和全部微博 ‘’’ preurl = url.split(“?”)
preurl = preurl[0]
pageurl = preurl+“?is_search=0&visible=0&is_all=1&is_tag=0&profile_ftype=1&page=”+str(num)+“#feedtop”
return pageurl
def spider(url,username,password):
num = 0;
browser = login(“http://weibo.com/login.php”,username,password)
print(“开始获取第1页的微博”)
html = getweibohtml(browser,url)
file = open(“f:****\\****xuanzi.txt”,“a”,encoding=“utf-8”)
user = getusermessage(html)
for key in user.keys():
file.write(key+“:”+user[key]+“\n”)
file.close()
num += getweibo(“f:****\\****xuanzi.txt”,html)
print(“获取第1页的微博成功”)
page = 1;
while True:
page+=1
pageurl = getpageurl(url,page)
print(pageurl)
html = getweibohtml(browser, pageurl)
if has_source(html):
print(“开始获取第”+str(page)+“页的微博”)
num += getweibo(“f:****\\****xuanzi.txt”,html)
else:
return
return num
if __name__ ==“__main__”:
#browser = login(“http://weibo.com/login.php”,“15271183269”,“f43312626”)
‘’’
for cookie in cookies: if cookie[“name”] == “_s_tentry”: log_cookie = cookie break ‘’’ ‘’’ getweibohtml(“http://www.weibo.com/u/1858002662?is_search=0&visible=0&is_tag=0&profile_ftype=1&page=3&c=spr_sinamkt_buy_hyww_weibo_t113&is_all=1”, browser) ‘’’ #print(has_source(browser,“http://www.weibo.com/u/1858002662?is_search=0&visible=0&is_tag=0&profile_ftype=1&page=21&c=spr_sinamkt_buy_hyww_weibo_t113&is_all=1”))
#file = open(“f:\\test5.html”,“rb”).read() #html = file.decode(“utf-8”,“ignore”) #getweibo(html)
#url = getpageurl(“http://weibo.com/dasima315?sudaref=passport.weibo.com&is_all=1”, 4) #print(url) #num = spider(“http://www.weibo.com/u/1858002662?c=spr_sinamkt_buy_hyww_weibo_p113&is_hot=1”, “15271183269”, “f43312626”) #print(“一共获取”+str(num)+“条微博”)
spider(“http://www.weibo.com/u/1858002662?c=spr_sinamkt_buy_hyww_weibo_p113&is_hot=1”,“15271183269”,“f43312626”)
轩子局二兔的微博一共有21页,932条,以下是一部分爬取到的文件内容
姓名:轩子巨2兔
头像:http://tvax3.sinaimg.cn/crop.9.0.493.493.180/6ebedee6ly8ffq59vzv9qj20e80dpaag.jpg
认证:斗鱼直播平台签约主播
关注:293
粉丝:503619
微博:932
第1条微博内容是:
庆祝微博40万关注转发这条骚气的微博,抽5人送出100元红包加一段私人定制音频
http://wx3.sinaimg.cn/orj360/6ebedee6gy1feecnoofjvg20ap07ikjr.gif
4333 1810 3644
第2条微博内容是:
来斗鱼快两个月了,昨夜斗鱼粉丝破了100万。黑粉很失望,他们竭尽全力却无法阻止。不膨胀,继续努力做好声音!为回馈百万轩子大军,抽5人送出100元红包和一段专属音频,转发即可,截止至3月25日24点。
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1fdve8bakcpj20sf0kuara.jpg
http://wx1.sinaimg.cn/thumb150/6ebedee6gy1fdve8jbtjwj20sf0kudz7.jpg
4431 1768 3184
第3条微博内容是:
“不知尬舞”
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1fdir07mn9jj21kw23ungz.jpg
http://wx3.sinaimg.cn/thumb150/6ebedee6gy1fdir0akk9vj21kw22ekad.jpg
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1fdir0ca1xaj21kw23ugzt.jpg
http://wx1.sinaimg.cn/thumb150/6ebedee6gy1fdir03x5dhj20k00ci77i.jpg
256 1281 5533
第4条微博内容是:
这次斗鱼的美食节目因故取消了,但不影响真爱粉的福利。转发抽5人送100网费
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1ff8qkcg02sj21be0qodn4.jpg
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1ff8qkfkf19j21be0qo47a.jpg
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1ff8qkk6zojj20qo1bedna.jpg
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1ff8qky81l8j20qo1be10q.jpg
http://wx1.sinaimg.cn/thumb150/6ebedee6gy1ff8qkh1k32j21be0qoaiu.jpg
http://wx4.sinaimg.cn/thumb150/6ebedee6gy1ff8qllalw1j222o160njp.jpg
http://wx3.sinaimg.cn/thumb150/6ebedee6gy1ff8qkil677j21be0qoqb6.jpg
http://wx2.sinaimg.cn/thumb150/6ebedee6gy1ff8qkwcdmej21be0qojy6.jpg
http://wx1.sinaimg.cn/thumb150/6ebedee6gy1ff8qlb89d1j216022ox1n.jpg
1929 1118 3457
第5条微博内容是:
最后
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!