看了楼主的代码,总结了一下思路:
百度文库比较特殊,虽然抓包可以抓得到,但是文章的api很难逆向,至少我不行。。。
楼主提供了一个很好的思路,就是简单粗暴的正则表达式匹配百度文库js自己生成的api地址,再用改地址请求真正的文章内容
1. 获得某篇文章的url之后,下载该url对应的网页数据,在该网页数据中含有真正文章的地址 WkInfo.htmlUrls = '{\x22ttf\x22:[{\x22pageIndex\x22:1,\x22param\x22:\x22&md5sum=2247898bde5831aa14cb0ff6976d581d&range=0-35283&sign=865b03efea\x22},{\x22pageIndex\x22:2,\x22param\x22:\x22&md5sum=2247898bde5831aa14cb0ff6976d581d&range=35284-&sign=865b03efea\x22}],\x22json\x22:[{\x22pageIndex\x22:1,\x22pageLoadUrl\x22:\x22https:\\\/\\\/wkbos.bdimg.com\\\/v1\\\/docconvert3256\\\/\\\/wk\\\/2247898bde5831aa14cb0ff6976d581d\\\/0.json?responseCacheControl=max-age%3D3888000&responseExpires=Wed%2C%2025%20Jul%202018%2013%3A45%3A30%20%2B0800&authorization=bce-auth-v1%2Ffa1126e91489401fa7cc85045ce7179e%2F2018-06-10T05%3A45%3A30Z%2F3600%2Fhost%2F3b600f217b1607544a7cce957b88c84d7d3ca5422dae74a2510f25a24335d268&x-bce-range=0-18105&token=b60d225474165f8613deafe1cbf02f87c4b17bfcc15b158c736120600be7f600&expire=2018-06-10T06:45:30Z\x22},{\x22pageIndex\x22:2,\x22pageLoadUrl\x22:\x22https:\\\/\\\/wkbos.bdimg.com\\\/v1\\\/docconvert3256\\\/\\\/wk\\\/2247898bde5831aa14cb0ff6976d581d\\\/0.json?responseCacheControl=max-age%3D3888000&responseExpires=Wed%2C%2025%20Jul%202018%2013%3A45%3A30%20%2B0800&authorization=bce-auth-v1%2Ffa1126e91489401fa7cc85045ce7179e%2F2018-06-10T05%3A45%3A30Z%2F3600%2Fhost%2F3b600f217b1607544a7cce957b88c84d7d3ca5422dae74a2510f25a24335d268&x-bce-range=18106-&token=b60d225474165f8613deafe1cbf02f87c4b17bfcc15b158c736120600be7f600&expire=2018-06-10T06:45:30Z\x22}],\x22png\x22:[{\x22pageIndex\x22:1,\x22pageLoadUrl\x22:\x22https:\\\/\\\/wkbos.bdimg.com\\\/v1\\\/docconvert3256\\\/\\\/wk\\\/2247898bde5831aa14cb0ff6976d581d\\\/0.png?responseCacheControl=max-age%3D3888000&responseExpires=Wed%2C%2025%20Jul%202018%2013%3A45%3A30%20%2B0800&authorization=bce-auth-v1%2Ffa1126e91489401fa7cc85045ce7179e%2F2018-06-10T05%3A45%3A30Z%2F3600%2Fhost%2Ff019623704c4d40895636a821d422e588f4843e0f007709d51eb75180696b573&x-bce-range=0-518&token=7276583f9571ef22b7465eb0b96fd1ef5a576fd7ee28b4d7f67b4bbcfc9e432d&expire=2018-06-10T06:45:30Z\x22},{\x22pageIndex\x22:2,\x22pageLoadUrl\x22:\x22https:\\\/\\\/wkbos.bdimg.com\\\/v1\\\/docconvert3256\\\/\\\/wk\\\/2247898bde5831aa14cb0ff6976d581d\\\/0.png?responseCacheControl=max-age%3D3888000&responseExpires=Wed%2C%2025%20Jul%202018%2013%3A45%3A30%20%2B0800&authorization=bce-auth-v1%2Ffa1126e91489401fa7cc85045ce7179e%2F2018-06-10T05%3A45%3A30Z%2F3600%2Fhost%2Ff019623704c4d40895636a821d422e588f4843e0f007709d51eb75180696b573&x-bce-range=519-&token=7276583f9571ef22b7465eb0b96fd1ef5a576fd7ee28b4d7f67b4bbcfc9e432d&expire=2018-06-10T06:45:30Z\x22}]}'
2. 使用正则表达式取得该文章的地址,可能不止一个https://wkbos.bdimg.com/v1/docconvert3256//wk/2247898bde5831aa14cb0ff6976d581d/0.json?responseCacheControl=max-age%3D3888000&responseExpires=Wed%2C%2025%20Jul%202018%2013%3A45%3A30%20%2B0800&authorization=bce-auth-v1%2Ffa1126e91489401fa7cc85045ce7179e%2F2018-06-10T05%3A45%3A30Z%2F3600%2Fhost%2F3b600f217b1607544a7cce957b88c84d7d3ca5422dae74a2510f25a24335d268&x-bce-range=0-18105&token=b60d225474165f8613deafe1cbf02f87c4b17bfcc15b158c736120600be7f600&expire=2018-06-10T06:45:30Z
3. 使用文章的地址再次向服务器请求,获得文章的数据,但是此时并不是文章的内容,是一个json结构的数据集
数据集与正则结果
4. 看到这个数据集我头皮发麻,不知道楼主是经历过怎样的绝望才能将其解析出来,辛苦了。。。
5. 该数据集是基于百度文库自己的阅读器生成的,其中包含了非常多的信息,文章内容就在其中,以五维的位置参数,标明内容出现的位置{"c":"\\u897f\\u534e\\u5bb6\\u4ff1\\u6709\\u9650\\u516c\\u53f8","p":{"h":24.977,"w":168.29,"x":257.79,"y":111.534,"z":210}
比如现在这样,内容就是 c 这一串的Unicode编码,p 就是其位置参数什么高啊宽啊
6. 最后就是楼主根据 y 的数值简单排列整个文档,解码保存
不得不说,楼主真的很牛X ,看完之后觉得不难,但是不看楼主的解析方法,我拿到数据集就是一脸懵逼,wtf??!
PS:谢谢楼主 |