python爬虫爬取微博知乎热搜榜

python爬虫爬取微博知乎热搜榜

使用python编写脚本爬取微博和知乎的热搜榜单,原理非常简单,构造http协议get请求(注意:get请求头部中需要包含cookie,否则无法获取到热搜数据,可以先使用浏览器访问,从开发者工具中找到对应cookie,并记录下来,以后一直使用这个cookie即可),再使用request工具包发送get请求,使用lxml工具解析返回的html,从中找出我们想要的数据即可。

请求连接

微博:https://weibo.com/a/hot/realtime
知乎:https://www.zhihu.com/hot

详情见代码,由于隐私问题,代码中不包含cookie信息,实际使用的时候需要添加上自己的cookie。

import requests
from lxml import html

def get_weibo_hot():
    headers={
    
    
	    'Host':'www.weibo.com',
	    'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
	    'Connection':'keep-alive',
	    'Pargma':'np-cache',
        'Cookie':,#添加cookie
	    'Cache-Control':'no-cache',
	    'Upgrade-Insecure-Requests':'1',
	    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	    'User-Agent':'Mozilla/5.0 (Macintosh; Inter Mac OS X 10_12_4) '
				    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    }

    url = "https://weibo.com/a/hot/realtime"
    resp = requests.get(url, headers=headers)
    page = resp.content
    root = html.fromstring(page)
    titles = root.xpath('//a[@class="S_txt1"]/text()')
    hots = titles[-6:-1]
    hots.extend(titles[0:15])
    print('=======================================================================================================================')
    print('============================================== 新浪微博实时热搜榜 =====================================================')
    print('=======================================================================================================================')
    for idx, title in enumerate(hots):
        print("%2d            %s" %(idx+1,title)) 
    print('=======================================================================================================================')

def get_zhihu_hot():
    headers={
    
    
	    'Host':'www.zhihu.com',
	    'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
	    'Connection':'keep-alive',
	    'Pargma':'np-cache',
        'Cookie':,#添加cookie
	    'Cache-Control':'no-cache',
	    'Upgrade-Insecure-Requests':'1',
	    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	    'User-Agent':'Mozilla/5.0 (Macintosh; Inter Mac OS X 10_12_4) '
				    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    }

    url = "https://www.zhihu.com/hot"
    resp = requests.get(url, headers=headers)
    page = resp.content
    root = html.fromstring(page)
    titles = root.xpath('//h2[@class="HotItem-title"]/text()')
    hots = titles[0:20]
    print('=======================================================================================================================')
    print('================================================ 知乎实时热搜榜 =======================================================')
    print('=======================================================================================================================')
    for idx, title in enumerate(hots):
        print("%2d            %s" %(idx+1,title)) 
    print('=======================================================================================================================')

get_weibo_hot()
print(" ")
get_zhihu_hot()

运行结果示例

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_38600065/article/details/106713652