进一步的爬取小说
看出什么了?没错第一章和第二章的html相差1,但是第二章和第三章的html相差很多,第三章和第四章的也是相差很多
这是最后一张的html网址,和第一章对比,发现两者相差1577770。如果直接用for i in range(15114818,166692588)的话,你就慢慢等着吧,中间也会出现异常。
那么怎么办呢?我们可以从目录页面获得每一章的html
易知,html在a标签中,那就读取这个a标签里面的东西
url='https://www.biquwu.cc/biquge/41_41813'
a=[]
html=requests.get(url).text
soup=BeautifulSoup(html,'lxml')
t=soup.find_all('a')
for titles in t://红字是核心,t是一个列表,for语句就是把列表里面的元素遍历一遍
link=titles['href']//从title中找到链接,把这个链接赋值给link。
try:
a.append(link)//尽量要用try,有时候爬虫会出现异常,把link给a列表
except:
Continue
接下来把一些错误的html链接给舍弃
copy=deepcopy(a)
for i in copy:
if('html'not in i)or('/'in i):
a.remove(i)
这个也是一个难点 对于我而言好吧。首先我们在爬取了目录网页的所有html链接后会发现一些杂七杂八的链接,这些链接不属于小说内容链接。所以必须的舍弃。那么第一个想法就是用remove函数,但是我之后发现remove函数不能完全的把那些乱七八糟的链接给舍弃。这是为什么?我从网上找到了原因。
remove函数是删除该列表的第一个函数
我们现在已经删除了a列表的第一个元素‘1’,
那么‘2’这个元素就放到的第一个地址,
而for语句已经执行了一次到a[1]这个地方,
a[1]现在是3,所以删除了‘3’这个元素而跳过了‘2’这个元素。
接下来定义一个函数,爬取具体的网址。
def get_text(all_url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
try:
html=requests.get(all_url,headers=headers,timeout=13,)timeout最好设置的时间长一点,有时候网址的爬取速度是不一样的,timeout设置少的话有些网站爬取就会失败
print(html)
html.encoding='gbk' 用GBK编码而不是其他的是因为用其他的编码会得到中文乱码或者一些乱七八糟的东西
soup=BeautifulSoup(html.text,'lxml')
content=soup.find_all('div',id='content')
寻找所有属性名为id,属性值为content的内容
s=''.join('%s'%id for id in content)
join() 方法用于将序列中的元素以指定的字符连接生成一个新的字符串,
首先将content列表里面的元素进行遍历,
然后将这个元素转化为字符串,
最后把这个字符串赋值给s。
s=s.replace('<br/>','\n')
s=s.replace('\xa0',' ')
s=s.replace('div id="content">','')
s=s.replace('<div>','')
把一些杂七杂八的东西用replace函数替代掉,并且把新的内容赋值给s
with open('灵魂导游.txt','a')as f:
f.write(s)
f.close()
print('写入成功')
sleep(0.5)
设置睡眠时间是为了保护被爬虫网站和减少网站发现爬虫的可能性,sleep函数需要import time数据库
except:
print('写入失败')
最后附代码
import os
import requests
from bs4 import BeautifulSoup
import re
from copy import deepcopy
from time import sleep
def get_text(all_url):
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
try:
html=requests.get(all_url,headers=headers,timeout=13,)
print(html)
html.encoding='gbk'
soup=BeautifulSoup(html.text,'lxml')
content=soup.find_all('div',id='content')
s=''.join('%s'%id for id in content)
s=s.replace('<br/>','\n')
s=s.replace('\xa0',' ')
s=s.replace('div id="content">','')
s=s.replace('<div>','')
with open('灵魂导游.txt','a')as f:
f.write(s)
f.close()
print('写入成功')
i=i+1
sleep(0.5)
except:
print('写入失败')
url='https://www.biquwu.cc/biquge/41_41813'
a=[]
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
html=requests.get(url,headers=headers,timeout=20).text
soup=BeautifulSoup(html,'lxml')
t=soup.find_all('a')
q=1
for titles in t:
link=titles['href']
try:
a.append(link)
except:
continue
copy=deepcopy(a)
for i in copy:
if('html'not in i)or('/'in i):
a.remove(i)
for i in a:
all_url='https://www.biquwu.cc/biquge/41_41813'+'/'+str(i)
get_text(all_url)
print(all_url)