找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 152|回复: 13

看有人需要下百度文库的源码,来这有一份

[复制链接]

179

主题

1158

回帖

2931

积分

金牌会员

积分
2931
发表于 2019-3-15 22:35:52 | 显示全部楼层 |阅读模式
[ol]
  • import requests
  • import re
  • import json
  • import os
  • session = requests.session()
  • def fetch_url(url):
  •     return session.get(url).content.decode('gbk')
  • def get_doc_id(url):
  •     return re.findall('view/(.*).html', url)[0]
  • def parse_type(content):
  •     return re.findall(r"docType.*?:.*?\'(.*?)\',", content)[0]
  • def parse_title(content):
  •     return re.findall(r"title.*?:.*?\'(.*?)\',", content)[0]
  • def parse_doc(content):
  •     result = ''
  •     url_list = re.findall('(https.*?0.json.*?)\\\\x22}', content)
  •     url_list = [addr.replace("\\\\\\/", "/") for addr in url_list]
  •     for url in url_list[:-5]:
  •         content = fetch_url(url)
  •         y = 0
  •         txtlists = re.findall('"c":"(.*?)".*?"y":(.*?),', content)
  •         for item in txtlists:
  •             if not y == item[1]:
  •                 y = item[1]
  •                 n = '\n'
  •             else:
  •                 n = ''
  •             result += n
  •             result += item[0].encode('utf-8').decode('unicode_escape', 'ignore')
  •     return result
  • def parse_txt(doc_id):
  •     content_url = 'https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=' + doc_id
  •     content = fetch_url(content_url)
  •     md5 = re.findall('"md5sum":"(.*?)"', content)[0]
  •     pn = re.findall('"totalPageNum":"(.*?)"', content)[0]
  •     rsign = re.findall('"rsign":"(.*?)"', content)[0]
  •     content_url = 'https://wkretype.bdimg.com/retype/text/' + doc_id + '?rn=' + pn + '&type=txt' + md5 + '&rsign=' + rsign
  •     content = json.loads(fetch_url(content_url))
  •     result = ''
  •     for item in content:
  •         for i in item['parags']:
  •             result += i['c'].replace('\\r', '\r').replace('\\n', '\n')
  •     return result
  • def parse_other(doc_id):
  •     content_url = "https://wenku.baidu.com/browse/getbcsurl?doc_id=" + doc_id + "&pn=1&rn=99999&type=ppt"
  •     content = fetch_url(content_url)
  •     url_list = re.findall('{"zoom":"(.*?)","page"', content)
  •     url_list = [item.replace("\", '') for item in url_list]
  •     if not os.path.exists(doc_id):
  •         os.mkdir(doc_id)
  •     for index, url in enumerate(url_list):
  •         content = session.get(url).content
  •         path = os.path.join(doc_id, str(index) + '.jpg')
  •         with open(path, 'wb') as f:
  •             f.write(content)
  •     print("图片保存在" + doc_id + "文件夹")
  • def save_file(filename, content):
  •     with open(filename, 'w', encoding='utf8') as f:
  •         f.write(content)
  •         print('已保存为:' + filename)
  • def main():
  •     url = input('请输入要下载的文库URL地址_')
  •     content = fetch_url(url)
  •     doc_id = get_doc_id(url)
  •     Type = parse_type(content)
  •     title = parse_title(content)
  •     if Type == 'doc':
  •         result = parse_doc(content)
  •         save_file(title + '.txt', result)
  •     elif Type == 'txt':
  •         result = parse_txt(doc_id)
  •         save_file(title + '.txt', result)
  •     else:
  •         parse_other(doc_id)
  • if __name__ == "__main__":
  •     main()[/ol]复制代码







  • 回复

    使用道具 举报

    17

    主题

    1060

    回帖

    2185

    积分

    金牌会员

    积分
    2185
    发表于 2019-3-15 22:39:53 | 显示全部楼层
    战略性标记
    回复

    使用道具 举报

    5

    主题

    182

    回帖

    389

    积分

    中级会员

    积分
    389
    发表于 2019-3-15 22:40:38 | 显示全部楼层
    mark
    回复

    使用道具 举报

    40

    主题

    443

    回帖

    1026

    积分

    金牌会员

    积分
    1026
    发表于 2019-3-15 22:44:56 | 显示全部楼层
    战略mark
    回复

    使用道具 举报

    32

    主题

    477

    回帖

    1060

    积分

    金牌会员

    积分
    1060
    发表于 2019-3-15 22:45:22 | 显示全部楼层
    战略性mark
    回复

    使用道具 举报

    42

    主题

    418

    回帖

    984

    积分

    高级会员

    积分
    984
    发表于 2019-3-15 22:45:53 | 显示全部楼层
    本帖最后由 shares 于 2019-3-15 22:49 编辑

    看见了
    下载的是图片
    如果能是源文档就完美了
    还是谢谢了
    回复

    使用道具 举报

    3

    主题

    84

    回帖

    191

    积分

    注册会员

    积分
    191
    发表于 2019-3-15 22:46:05 | 显示全部楼层
    不是可以直接下载原文档吗
    回复

    使用道具 举报

    1

    主题

    22

    回帖

    57

    积分

    注册会员

    积分
    57
    发表于 2019-3-15 22:47:35 | 显示全部楼层
    楼主666啊,标记一下
    回复

    使用道具 举报

    0

    主题

    101

    回帖

    228

    积分

    中级会员

    积分
    228
    发表于 2019-3-15 22:48:03 | 显示全部楼层
    来个教程啊标记

    回复

    使用道具 举报

    0

    主题

    1

    回帖

    4

    积分

    新手上路

    积分
    4
    发表于 2019-3-15 22:49:15 | 显示全部楼层
    学呀,今晚技术大牛多
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Discuz! X

    GMT+8, 2025-5-9 14:20 , Processed in 0.094877 second(s), 4 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

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