鱼C论坛

 找回密码
 立即注册
查看: 933|回复: 5

[已解决]毕业设计其中一环:在固定网页爬取相关内容并建库

[复制链接]
发表于 2019-3-14 17:40:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最近在搞毕业设计其中有一环需要爬取一个百度文库文章上的固定内容,需要写程序来解决, https://wenku.baidu.com/view/26f68a9268dc5022aaea998fcc22bcd126ff427f.html?from=search  就是把这个文库里的内容爬取下来 并建立成表格的形式(能保存到电脑里也行),忘各位大神鼎力相助!
最佳答案
2019-3-14 20:56:50
以前看到论坛的一位大佬写的

  1. import requests
  2. import re
  3. import json
  4. import os

  5. session = requests.session()


  6. def fetch_url(url):
  7.     return session.get(url).content.decode('gbk')


  8. def get_doc_id(url):
  9.     return re.findall('view/(.*).html', url)[0]


  10. def parse_type(content):
  11.     return re.findall(r"docType.*?\:.*?\'(.*?)\'\,", content)[0]


  12. def parse_title(content):
  13.     return re.findall(r"title.*?\:.*?\'(.*?)\'\,", content)[0]


  14. def parse_doc(content):
  15.     result = ''
  16.     url_list = re.findall('(https.*?0.json.*?)\\\\x22}', content)
  17.     url_list = [addr.replace("\\\\\\/", "/") for addr in url_list]
  18.     for url in url_list[:-5]:
  19.         content = fetch_url(url)
  20.         y = 0
  21.         txtlists = re.findall('"c":"(.*?)".*?"y":(.*?),', content)
  22.         for item in txtlists:
  23.             if not y == item[1]:
  24.                 y = item[1]
  25.                 n = '\n'
  26.             else:
  27.                 n = ''
  28.             result += n
  29.             result += item[0].encode('utf-8').decode('unicode_escape', 'ignore')
  30.     return result


  31. def parse_txt(doc_id):
  32.     content_url = 'https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=' + doc_id
  33.     content = fetch_url(content_url)
  34.     md5 = re.findall('"md5sum":"(.*?)"', content)[0]
  35.     pn = re.findall('"totalPageNum":"(.*?)"', content)[0]
  36.     rsign = re.findall('"rsign":"(.*?)"', content)[0]
  37.     content_url = 'https://wkretype.bdimg.com/retype/text/' + doc_id + '?rn=' + pn + '&type=txt' + md5 + '&rsign=' + rsign
  38.     content = json.loads(fetch_url(content_url))
  39.     result = ''
  40.     for item in content:
  41.         for i in item['parags']:
  42.             result += i['c'].replace('\\r', '\r').replace('\\n', '\n')
  43.     return result


  44. def parse_other(doc_id):
  45.     content_url = "https://wenku.baidu.com/browse/getbcsurl?doc_id=" + doc_id + "&pn=1&rn=99999&type=ppt"
  46.     content = fetch_url(content_url)
  47.     url_list = re.findall('{"zoom":"(.*?)","page"', content)
  48.     url_list = [item.replace('\\','') for item in url_list]
  49.     if not os.path.exists(doc_id):
  50.         os.mkdir(doc_id)
  51.     for index, url in enumerate(url_list):
  52.         content = session.get(url).content
  53.         path = os.path.join(doc_id, str(index) + '.jpg')
  54.         with open(path, 'wb') as f:
  55.             f.write(content)
  56.     print("图片保存在" + doc_id + "文件夹")


  57. def save_file(filename, content):
  58.     with open(filename, 'w', encoding='utf8') as f:
  59.         f.write(content)
  60.         print('已保存为:' + filename)


  61. def main():
  62.     url = input('请输入要下载的文库URL地址_')
  63.     content = fetch_url(url)
  64.     doc_id = get_doc_id(url)
  65.     type = parse_type(content)
  66.     title = parse_title(content)
  67.     if type == 'doc':
  68.         result = parse_doc(content)
  69.         save_file(title + '.txt', result)
  70.     elif type == 'txt':
  71.         result = parse_txt(doc_id)
  72.         save_file(title + '.txt', result)
  73.     else:
  74.         parse_other(doc_id)


  75. if __name__ == "__main__":
  76.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-14 19:00:42 | 显示全部楼层
请问你写出来了吗?可以分享一下吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-14 20:56:27 | 显示全部楼层
这个文章不能做表格吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-14 20:56:50 | 显示全部楼层    本楼为最佳答案   
以前看到论坛的一位大佬写的

  1. import requests
  2. import re
  3. import json
  4. import os

  5. session = requests.session()


  6. def fetch_url(url):
  7.     return session.get(url).content.decode('gbk')


  8. def get_doc_id(url):
  9.     return re.findall('view/(.*).html', url)[0]


  10. def parse_type(content):
  11.     return re.findall(r"docType.*?\:.*?\'(.*?)\'\,", content)[0]


  12. def parse_title(content):
  13.     return re.findall(r"title.*?\:.*?\'(.*?)\'\,", content)[0]


  14. def parse_doc(content):
  15.     result = ''
  16.     url_list = re.findall('(https.*?0.json.*?)\\\\x22}', content)
  17.     url_list = [addr.replace("\\\\\\/", "/") for addr in url_list]
  18.     for url in url_list[:-5]:
  19.         content = fetch_url(url)
  20.         y = 0
  21.         txtlists = re.findall('"c":"(.*?)".*?"y":(.*?),', content)
  22.         for item in txtlists:
  23.             if not y == item[1]:
  24.                 y = item[1]
  25.                 n = '\n'
  26.             else:
  27.                 n = ''
  28.             result += n
  29.             result += item[0].encode('utf-8').decode('unicode_escape', 'ignore')
  30.     return result


  31. def parse_txt(doc_id):
  32.     content_url = 'https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=' + doc_id
  33.     content = fetch_url(content_url)
  34.     md5 = re.findall('"md5sum":"(.*?)"', content)[0]
  35.     pn = re.findall('"totalPageNum":"(.*?)"', content)[0]
  36.     rsign = re.findall('"rsign":"(.*?)"', content)[0]
  37.     content_url = 'https://wkretype.bdimg.com/retype/text/' + doc_id + '?rn=' + pn + '&type=txt' + md5 + '&rsign=' + rsign
  38.     content = json.loads(fetch_url(content_url))
  39.     result = ''
  40.     for item in content:
  41.         for i in item['parags']:
  42.             result += i['c'].replace('\\r', '\r').replace('\\n', '\n')
  43.     return result


  44. def parse_other(doc_id):
  45.     content_url = "https://wenku.baidu.com/browse/getbcsurl?doc_id=" + doc_id + "&pn=1&rn=99999&type=ppt"
  46.     content = fetch_url(content_url)
  47.     url_list = re.findall('{"zoom":"(.*?)","page"', content)
  48.     url_list = [item.replace('\\','') for item in url_list]
  49.     if not os.path.exists(doc_id):
  50.         os.mkdir(doc_id)
  51.     for index, url in enumerate(url_list):
  52.         content = session.get(url).content
  53.         path = os.path.join(doc_id, str(index) + '.jpg')
  54.         with open(path, 'wb') as f:
  55.             f.write(content)
  56.     print("图片保存在" + doc_id + "文件夹")


  57. def save_file(filename, content):
  58.     with open(filename, 'w', encoding='utf8') as f:
  59.         f.write(content)
  60.         print('已保存为:' + filename)


  61. def main():
  62.     url = input('请输入要下载的文库URL地址_')
  63.     content = fetch_url(url)
  64.     doc_id = get_doc_id(url)
  65.     type = parse_type(content)
  66.     title = parse_title(content)
  67.     if type == 'doc':
  68.         result = parse_doc(content)
  69.         save_file(title + '.txt', result)
  70.     elif type == 'txt':
  71.         result = parse_txt(doc_id)
  72.         save_file(title + '.txt', result)
  73.     else:
  74.         parse_other(doc_id)


  75. if __name__ == "__main__":
  76.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-28 15:14:02 | 显示全部楼层
咕咕鸡鸽鸽 发表于 2019-3-14 20:56
以前看到论坛的一位大佬写的

老哥,第44行到56行你知道大致含义吗?不胜感激
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-28 17:30:28 | 显示全部楼层
wangyaoxin 发表于 2019-4-28 15:14
老哥,第44行到56行你知道大致含义吗?不胜感激

https://fishc.com.cn/forum.php?m ... 9%B6%C8%CE%C4%BF%E2

帮你找了一下,这是原作者,你可以问一下,我也不太懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 09:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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