python 跨知乎app发私信以及Python专栏30万用户信息爬取

import requests
class SendMsg:
    def __init__(self):
        self.url='https://www.zhihu.com/api/v4/messages'
        #要发送的信息
        self.data={'content':'小管家你好',
            'receiver_hash':'****',#这里是每个用户的哈希值ID
            'type':'common'
        }
        self.cookie={****}
    def postmsg(self):
        '''主要用于发送私信的方法requests.request(method, url, **kwargs)
            支持各种方法  400请求错误 401验证不通过
            用data还是json提交,看请求头content_type是不是json
            cookies中需要token,不能从原始头复制'''
        html=requests.request('POST',self.url,json=self.data,headers=self.header,cookies=self.cookie)
        print(html.status_code)
        print(html.json())

for i in range(2):
    sending=SendMsg()
    sending.postmsg()
#附上一个知乎用户数据爬取
import requests

class GetMsg:
    '''获取粉丝信息'''
    def __init__(self):
        self.cookie = {'*****'}
        self.header={'User-Agent':'***',
                     }
    def getMag(self):
        for i in range(120,140,20):
            url='https://www.zhihu.com/api/v4/topics/19552832/followers?include=data[*]' \
                '.gender,answer_count,articles_count,follower_count,' \
                'is_following,is_followed&limit=20&offset={}'.format(i)
            html=requests.request('GET',url=url,cookies=self.cookie,headers=self.header)
            print(html.status_code)
            content=html.json()
            for c in content['data']:
                if c['gender']==1:
                    print('男')
                else:
                    print('女')
                print(c['name'], c['headline'])


a=GetMsg()
a.getMag()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python爬取知乎用户信息,你可以按照以下步骤进行操作: 1. 安装必要的库:使用`pip`命令安装`requests`和`beautifulsoup4`库。 2. 送请求获取页面:使用`requests`库送HTTP请求,获取知乎用户信息页面的HTML内容。 3. 解析页面内容:使用`beautifulsoup4`库解析页面内容,提取所需的用户信息。 下面是一个简单的示例代码,展示如何爬取知乎用户信息: ```python import requests from bs4 import BeautifulSoup def get_user_info(user_url): # 送请求获取页面内容 response = requests.get(user_url) html_content = response.text # 解析页面内容 soup = BeautifulSoup(html_content, 'html.parser') user_name = soup.select_one('.ProfileHeader-name').text.strip() user_bio = soup.select_one('.ProfileHeader-headline').text.strip() user_location = soup.select_one('.ProfileHeader-infoItem.ProfileHeader-location').text.strip() # 返回用户信息 return { 'name': user_name, 'bio': user_bio, 'location': user_location } # 示例:爬取知乎用户「知乎小助手」的信息 user_url = 'https://www.zhihu.com/people/zhihuassistant' user_info = get_user_info(user_url) print(user_info) ``` 请注意,该示例仅爬取了用户的名称、个人简介和所在地信息。你可以根据自己的需求修改代码,提取其他感兴趣的用户信息。此外,为了遵守网站的使用规则,请确保在爬取数据时尊重知乎的限制,并遵守相关的法律法规。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值