知乎爬虫之爬取专栏信息

本文介绍了如何使用爬虫抓取知乎专栏https://zhuanlan.zhihu.com/Entertainmentlaw的信息,包括专栏名称、作者和关注人数。通过分析请求URL和消息头,实现了对文章信息的抓取,揭示了知乎加载文章的limit和offset参数规律。
摘要由CSDN通过智能技术生成

接着昨天的模拟登陆,今天来爬取一下专栏信息
我们将对专栏https://zhuanlan.zhihu.com/Entertainmentlaw进行抓取
首先还是进行抓包分析,可以发现这里有我们想要的专栏的名称,作者,关注人数等信息
可以发现这里有我们想要的专栏的名称,作者,关注人数等信息
然后我们看一下消息头,看一下请求的URL和请求头
这里写图片描述
然后就可以编写代码了

# -*- coding:utf-8 -*-
__author__="weikairen"

import  requests
from bs4 import  BeautifulSoup
import  time

BASE_URL='https://www.zhihu.com/'
LOGIN_URL=BASE_URL+'login/phone_num'
CAPTCHA_URL=BASE_URL+'captcha.gif?r='+str(int(time.time())*1000)+'&type=login'

BLOGS_BASE_URL='https://zhuanlan.zhihu.com/Entertainmentlaw'
BLOGS_API_URL='https://zhuanlan.zhihu.com/api/columns/Entertainmentlaw'

session = requests.session()    #session创建为全局变量是为了能在不同的函数中使用一个相同的session
#在登录过后 session会保存服务器返回的cookie,爬取专栏信息的时候用这个session,服务器就会认为你已经登录,就不会拒绝你的请求了

def login():
    headers={
        'host':'www.zhihu.com',
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0',
        'referer':"https://www.zhihu.com/",
        'X - Requested - With': "XMLHttpRequest"
    }                                                      #构造请求头,
可以使用 Python 的 Requests 和 BeautifulSoup 库来爬取知乎用户信息。首先需要登录知乎获取 cookie,然后通过模拟登录获取到用户的个人主页,再使用 BeautifulSoup 解析页面获取用户信息。 以下是示例代码: ```python import requests from bs4 import BeautifulSoup # 登录知乎并获取 cookie session = requests.Session() headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} login_url = 'https://www.zhihu.com/signin' response = session.get(login_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') _xsrf = soup.find('input', attrs={'name': '_xsrf'})['value'] captcha_url = soup.find('img', attrs={'class': 'Captcha-englishImg'})['src'] # 模拟登录获取用户信息 login_data = { '_xsrf': _xsrf, 'email': 'your_account', 'password': 'your_password', 'captcha': input('请输入验证码' + captcha_url), 'remember_me': 'true' } session.post(login_url, headers=headers, data=login_data) user_url = 'https://www.zhihu.com/people/username' response = session.get(user_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 解析页面获取用户信息 name = soup.find('span', attrs={'class': 'ProfileHeader-name'}).text headline = soup.find('span', attrs={'class': 'RichText ztext ProfileHeader-headline'}).text description = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-description'}).find('span', attrs={'class': 'RichText ztext'}).text.strip() location = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-location'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() business = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-business'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() employment = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-employment'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() position = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-position'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() education = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-education'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() major = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-major'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() ``` 以上代码中,需要替换 `your_account` 和 `your_password` 为你的知乎登录账号和密码,并将 `username` 替换为你要爬取的用户的用户名。另外,为了防止被知乎爬虫机制检测到,最好加上一些随机的等待时间和 User-Agent 等信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值