其他分享
首页 > 其他分享> > 爬取微博热搜榜

爬取微博热搜榜

作者:互联网

1.打开微博网页:https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6

2.右键打开网页源代码

 

3.导入相应的库

4.编写代码

import requests
from lxml import etree

url="https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6"
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}

def main():
    html=etree.HTML(requests.get(url,headers=header).text)
    rank=html.xpath('//td[@class="td-01 ranktop"]/text()')
    affair=html.xpath('//td[@class="td-02"]/a/text()')
    view = html.xpath('//td[@class="td-02"]/span/text()')
    top=affair[0]
    affair=affair[1:]
    print('{0:<10}\t{1:<40}'.format("top",top))
    for i in range(0, len(affair)):
        print("{0:<10}\t{1:{3}<30}\t{2:{3}>20}".format(rank[i],affair[i],view[i],chr(12288)))
main()

5.输出结果

 

 

 6.热搜图

 

标签:affair,text,搜榜,class,爬取,html,td,top,微博热
来源: https://www.cnblogs.com/LLLLHHH/p/12533921.html