温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用python项目对百度文库进行爬取并下载

发布时间:2020-12-04 15:18:31 来源:亿速云 阅读:351 作者:Leah 栏目:开发技术

这篇文章给大家介绍利用python项目对百度文库进行爬取并下载,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

具体方法如下:

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)


# test_txt_url = 'https://wenku.baidu.com/view/cbb4af8b783e0912a3162a89.html?from=search'
# test_ppt_url = 'https://wenku.baidu.com/view/2b7046e3f78a6529657d5376.html?from=search'
# test_pdf_url = 'https://wenku.baidu.com/view/dd6e15c1227916888586d795.html?from=search'
# test_xls_url = 'https://wenku.baidu.com/view/eb4a5bb7312b3169a551a481.html?from=search'
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()

关于利用python项目对百度文库进行爬取并下载就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI