百度文库爬虫
我们在开始爬虫之前,首先要了解网页的信息和结构,然后获取请求的url和参数(payloads)来得到所要的数据并处理。接下来我们根据:
一.网页分析
二.爬虫编写
三.运行与结果
四.程序改善
一.网页分析
1.初步分析
我们首先以.txt文本文件来分析网站,从简单到复杂,从txt—>word—pdf等
我们以<>进行说明
from IPython.display import Image
Image("./Images/1.png",width="600px",height="400px")
from IPython.display import Image
Image("./Images/0.png",width="600px",height="400px")
该百度文库的网址为:链接
URL:https://wenku.baidu.com/view/9a41886f26d3240c844769eae009581b6bd9bd6e.html?fr=search
查看该网页的源代码,并没有我们所要的文本数据,但是有我们需要的参数如下:
title:文件主题
docId:文件id。这个参数很重要,我们下一步需要它跳转到其他网址
docType:文件类型
from IPython.display import Image
Image("./Images/2.png",width="600px",height="400px")
前提说明:对于上面数据的获取需要re正则表达式,对于别的网页分析器,比如lxml,bs4等不适合这样格式的数据。对于正则表达式的学习,可以参考链接:正则表达式
既然上个url中没有我们需要的数据,那么我们进行浏览器Network进行调试,寻找我们需要的数据。我们要明白一点,数据不可能无缘无故的出现在网页中,肯定存在url将数据发送过来,此数据格式大概率为JSON
2.数据发现
经过url的查找,我们终于找到我们需要的数据链接
from IPython.display import Image
Image("./Images/3.png",width="600px",height="400px")
上面url所需的参数如下:
md5sum: de679a1e4f76df8ac8366bd7663285d6
sign: 91f37496c3
callback: cb
pn: 1
rn: 4
type: txt
rsign: p_4-r_0-s_896f2
_: 1587883180674
from IPython.display import Image
Image("./Images/4.png",width="600px",height="400px")
将以上两个重要的URL进行对比:
https://wenku.baidu.com/view/9a41886f26d3240c844769eae009581b6bd9bd6e.html?fr=search
https://wkretype.bdimg.com/retype/text/9a41886f26d3240c844769eae009581b6bd9bd6e?md5sum=de679a1e4f76df8ac8366bd7663285d6&sign=91f37496c3&callback=cb&pn=1&rn=4&type=txt&rsign=p_4-r_0-s_896f2&_=1587886244900
观察发现,其中文章的id(“9a41886f26d3240c844769eae009581b6bd9bd6e”)是一样的,但是文件内容还有几个参数(如md5sum,sign,rsgin,callback,pn等)
看看需获取文字详情的参数应该通过某个接口请求到的。按时间的顺序往上找,我们发现一个接口包括了需要的md5sum,sign,rsign等参数。其url为:https://wenku.baidu.com/api/doc/getdocinfo?callback=cb&doc_id=9a41886f26d3240c844769eae009581b6bd9bd6e&t=1587886245267&_=1587886244899
from IPython.display import Image
Image("./Images/5.png",width="600px",height="400px")
from IPython.display import Image
Image("./Images/6.png",width="600px",height="400px")
md5sum: de679a1e4f76df8ac8366bd7663285d6(已获取)
sign: 91f37496c3(已获取)
callback: cb
pn: 1
rn: 4
type: txt(已获取)
rsign: p_4-r_0-s_896f2(已获取)
_: 1587883180674
3.其他参数
通过上面的数据获取,我们还有以下参数未获取:
callback: cb
pn: 1
rn: 4
_: 1587883180674
callback参数是不变的为cb,pn为页数相关的,_可以猜到这个是类似时间戳的参数,对于这种前后关联的请求,处理时一般使用Requests的Session
session=requests.session()
session.get(url).content.decode('gbk')
对此我们网页信息分析和参数获取过程已经完毕,接下来我们进行程序编程
二.爬虫编写
首先我们编程实现爬取过程,先进行过程调试,然后进行函数编程
通过上面的分析,整个百度文库的爬取过程可以总结为请求百度文库URL,判断文章类型,请求getcodeinfo接口相关参数,请求百度文库正文接口
1.请求百度文库URL
import requests
import re
import json
import os
session=requests.session()
url=input("请输入要下载的文库URL地址:")
请输入要下载的文库URL地址: https://wenku.baidu.com/view/9a41886f26d3240c844769eae009581b6bd9bd6e.html?fr=search
content=session.get(url).content.decode('gbk')
pattern=re.compile('view/(.*?).html')
res=pattern.findall(url)[0]
res
'9a41886f26d3240c844769eae009581b6bd9bd6e'
doc_id=re.findall('view/(.*?).html',url)[0]
doc_id
'9a41886f26d3240c844769eae009581b6bd9bd6e'
2.判断文章类型
types=re.findall(r"'docType.*?\':.*?\'(.*?)\',",content)[0]
types
'txt'
title=re.findall(r"'title.*?\':.*?\'(.*?)\',",content)[0]
title
'一个循环网络爬虫'
3.请求getcodeinfo接口相关参数
content_url='https:/