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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4053|回复: 8
收起左侧

[Python 转载] 某乎日报爬虫——给问答区的朋友

[复制链接]
ermao 发表于 2019-6-17 20:58
本帖最后由 ermao 于 2019-6-19 21:46 编辑

问答区看到的,正好练手了,
没有反爬,加个ua就OK了

[Python] 纯文本查看 复制代码
import requests
import json
import threading
import time
import os
import re

paper_id = []
threads = 0
glock = threading.Lock()
header = {
    'User-Agent':
    'DailyApi/4 (Linux; Android 5.1.1; xiaomi 6 Build/xiaomi/xiaomi 6/x86/LMY48Z/zh_CN) Google-HTTP-Java-Client/1.22.0 (gzip) Google-HTTP-Java-Client/1.22.0 (gzip)'
}
path = ''


def get_single_paper(paper_id):

    global threads
    glock.acquire()
    threads += 1
    glock.release()

    url = 'https://news-at.zhihu.com/api/4/story/' + paper_id
    res = requests.get(url, headers=header)
    resJson = json.loads(res.content.decode('utf-8'))
    try:
        title = resJson['title']
        body = resJson['body']
        # css = resJson['css']  # css文件加了没什么效果,不加了
        # allcss = ''
        # for csss in css:
        #     allcss = allcss + '<link href="' + css + '"/>'
    except TypeError:
        print('json读取失败')
    else:
        f = open('./' + path + '/' + paper_id + '-' +
                 re.sub(r'[\\/:\*\?"<>\|]', '', title) + '.html',
                 'w',
                 encoding='utf-8')  # 文件名为id+标题.html
        f.write(body)  # 各个文件独立,不需要加锁
        f.close()

    glock.acquire()
    threads -= 1
    glock.release()
    return


def get_papers(id, timestamp_end):
    global path
    timestamp = 0
    if id == '35':
        path = '小事'
    elif id == '2':
        path = '瞎扯-吐槽'
    if not os.path.exists(path):
        os.makedirs(path)
    while True:
        if timestamp > timestamp_end:
            url = 'https://news-at.zhihu.com/api/4/section/' + id + '/before/'
            res = requests.get(url + str(timestamp), headers=header)
        elif timestamp == 0:
            url = 'https://news-at.zhihu.com/api/4/section/' + id
            res = requests.get(url, headers=header)
        else:
            print('已到日期上限')
            break
        print(res.content.decode('utf-8'))
        resJson = json.loads(res.content.decode('utf-8'))
        timestamp = resJson['timestamp']
        # n = len(resJson['stories'])
        # for i in range(0, n):
        for storie in resJson['stories']:
            paper_id.append(storie['id'])
    return len(paper_id)


def thread_control(N):
    for iii in paper_id:
        p_id = str(iii)
        t = threading.Thread(target=get_single_paper, args=(p_id, ))
        t.start()
        # print('当前线程数:{:^5}'.format(threads))
        while threads >= N:
            time.sleep(0.2)
    t.join()
    print('已完成')
    return


# 35 小事
# 2  瞎扯·如何正确的吐槽
# 1490536800 20170306之前的数据
# 1553608800 20190306之前的数据
nums = get_papers('2', 1490536800)  # 第一个参数是文章类别id,第二个参数是时间上限
print('ID采集完成,共' + str(nums) + '个,开始下载')
thread_control(20)  # 线程数
# get_single_paper('9712276')


截图:

22.png

免费评分

参与人数 6吾爱币 +7 热心值 +6 收起 理由
泳诗 + 1 我很赞同!
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
Tc5230 + 1 + 1 我很赞同!
为了青春 + 1 + 1 用心讨论,共获提升!
TIS有趣的灵魂 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
vethenc + 1 + 1 技术宅男真心强

查看全部评分

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

zjk414 发表于 2019-6-17 21:51
感谢发布原创作品
51PJcpuyong 发表于 2019-6-17 22:17
cyhcuichao 发表于 2019-6-18 02:07
jghdhr 发表于 2019-6-18 06:51
感谢分享,向大佬学习
626442267 发表于 2019-6-18 08:51
支持原创
jason_gat 发表于 2019-6-18 11:34
感谢分享,学习下
a8692375 发表于 2019-6-18 13:51
感谢分享~!!
iwzw 发表于 2019-6-18 16:15
感谢发布作品,学习了
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-4-24 16:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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