吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2038|回复: 23
收起左侧

[Python 转载] 【新手帖】爬取知乎收藏夹

[复制链接]
FUEL 发表于 2022-9-12 11:33
需要输入收藏夹编码,编码为收藏夹网址的最后的一串数字。
不多说,上代码。
[Python] 纯文本查看 复制代码
import datetime
import time
import requests
import re
collections=str(input("请输入收藏夹编码:\n"))
start=str(input("请输入爬取开始篇数:\n"))
end=str(input("请输入爬取结束篇数:\n"))
name_=str(input("请输入保存名字:\n"))
p=20
now_time = int(time.time())
timeArray = time.localtime(now_time)
otherStyleTime = str(time.strftime("%Y-%m-%d %H:%M:%S", timeArray))
with open("爬取日志.txt","a+",encoding="utf-8") as c:
    c.write("\n\n在  "+str(otherStyleTime)+"  从ID:  "+str(collections)+"  的收藏夹中爬取了从  "+str(start)+"  到  "+str(end)+"  页的数据\n\n")
for x in range(int(start),int(end),int(p)+1):
    url=f"https://www.zhihu.com/api/v4/collections/{collections}/items?offset={str(int(x)-1)}&limit=20"
    headers={
        "refer":f"https://www.zhihu.com/collection/{collections}",
        "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
    }
    s=requests.session()
    res=s.get(url=url,headers=headers).text
    # res_1=re.findall('"content":".*?","title":"","excerpt"',res,re.S)
    res = re.sub(r'\\u003c', "<", res)
    res = re.sub(r'\\u003e', ">", res)
    res = re.sub(r'\\n', "<br/>", res)
    res = re.sub(r'\\u0026', "&", res)  # 乱码替换
    # print(res)
    res_1=re.findall('"id":[0-9]+,"title":".*?","title":"","excerpt"',res,re.S)  #内容分割
    for res_2 in res_1:
        print("==============================这是第" + str(x) + "篇===============================")
        res_3 = re.findall('"title":".*?"', res_2, re.S)[0]   #标题截取
        otherStyleTime_2 = str(time.strftime("%Y年%m月%d日 %H时%M分%S秒", timeArray))
        with open(f"{name_}+{collections}+{otherStyleTime_2}.html","ab+") as f:
            y="<h1>"+"========这是第" + str(x) + "篇=======" + "</h1>"  #分割线赋值
            f.write(y.encode())  #分割线编码写入
            f.write(res_3.encode())  #标题编码写入
            print(res_3)     #标题打印
        res_4 = re.findall('"content":".*?","title":"","excerpt"', res_2, re.S)[0]   #切出主要内容
        res_5 = re.sub(r'"content":"', '<br/>"content":"', res_4)  #内容前空行
        res_5 = re.sub(r'","title":"","excerpt"', "", res_5)  #不必要的符号替换
        res_5 = re.sub(r'src=\\', "src=", res_5) #修改图片储存地址,方便图片显示
        with open(f"{name_}+{collections}+{otherStyleTime_2}.html","ab+") as f:
            f.write(res_5.encode())  #主要内容写入
        x=x+1
            # s='<br/>'+"="*20+"<br/>"+"<br/>"
            # f.write(s.encode())
        # print(res_5)
        # print("<br/>"+"="*50+"<br/>")
num_=x-1
print(f"恭喜你成功爬取了{num_}篇内容,爬取文件与python程序在同一个目录")
                                                                                                                                       

如图所示

如图所示

免费评分

参与人数 3吾爱币 +9 热心值 +1 收起 理由
ricardo172 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
yunce + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

shadmmd 发表于 2022-9-12 11:53
你的收藏夹成功引起了我的注意力。
子子木木木子子 发表于 2022-9-29 14:12
友情提示如果出现: UnicodeEncodeError: 'locale' codec can't encode character  错误, 添加代码
[Python] 纯文本查看 复制代码
import locale

locale.setlocale(locale.LC_CTYPE, 'Chinese')
jimmy007 发表于 2022-9-12 12:34
NvidiaChina 发表于 2022-9-12 13:11
这也太有才了吧
你是我的人 发表于 2022-9-12 13:54
感谢分享
sgbyg 发表于 2022-9-12 16:23
感谢分享
fkkrn-egqff.gif
arq2020 发表于 2022-9-12 17:26
楼主的收藏夹有点东西
shaunkelly 发表于 2022-9-12 18:01
其实楼主是想把图片给大家看的?
dmtm888 发表于 2022-9-12 18:38
感谢分享!!!
YZ520yy 发表于 2022-9-12 18:56

前排混脸熟,遵守班规只有回贴的份
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 19:37

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表