python 爬取百度文库并下载(免费文章限定)
作者:Jack-Cherish 时间:2022-05-08 16:18:28
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()
爬取结果
来源:https://github.com/Jack-Cherish/python-spider
标签:python,百度文库,爬虫
0
投稿
猜你喜欢
Python类的继承、多态及获取对象信息操作详解
2023-09-09 21:27:52
Python实现点阵字体读取与转换的方法
2022-11-08 08:33:27
深入理解mysql的自连接和join关联
2024-01-21 11:40:01
Window10上Tensorflow的安装(CPU和GPU版本)
2021-05-13 19:18:37
Windows8下安装Python的BeautifulSoup
2022-05-30 15:03:06
使用虚拟机在VirtualBox+openEuler上安装部署openGauss数据库
2024-01-21 22:45:53
Python图像处理之图像量化处理详解
2023-06-09 15:07:30
常用SQL功能语句
2024-01-15 17:08:58
Python 中的集合和字典
2021-03-18 22:53:30
详解Python中生成随机数据的示例详解
2021-12-21 03:56:17
ASP中的面向对象类
2011-04-11 10:34:00
python区块链实现简版工作量证明
2021-03-21 18:08:22
asp如何用HtmlEncode来显示Unicode编码?
2010-06-12 12:49:00
Laravel框架实现定时发布任务的方法
2023-11-22 23:54:57
详解pandas使用drop_duplicates去除DataFrame重复项参数
2021-09-12 12:36:26
教你怎么用Python操作MySql数据库
2024-01-13 06:46:48
得到字符串真实长度和取固定长度的字符串函数
2008-10-06 13:12:00
PHP OPP机制和模式简介(抽象类、接口和契约式编程)
2023-11-21 10:53:53
python绘制简单彩虹图
2022-09-06 04:55:14
对Python发送带header的http请求方法详解
2022-02-06 11:44:59