代码如下,爬了15章就报错,怎么搞?
from bs4.builder import HTMLTreeBuilder
import requests
import time
from tqdm import tqdm
from bs4 import BeautifulSoup
def get_content(target):
req = requests.get(url = target)
req.encoding = 'utf-8'
html = req.text
bf = BeautifulSoup(html, 'lxml')
texts = bf.find('div', id='content1')
content = texts.text.strip().split('\xa0'*4)
return content
if __name__ == '__main__':
server = 'https://www.xuanshu.com/book/78071/'
book_name = 'D://txt/掌门师叔不可能是凡人.txt'
target = 'https://www.xuanshu.com/book/78071/'
#html = open('D://txt/url.html','r',encoding='utf-8')
req = requests.get(url = target)
req.encoding = 'utf-8'
html = req.text
chapter_bs = BeautifulSoup(html, 'lxml')
chapters = chapter_bs.find('div', class_='pc_list')
chapters = chapters.find_all('a')
for chapter in tqdm(chapters):
chapter_name = chapter.string
url = server + chapter.get('href')
content = get_content(url)
with open(book_name, 'a', encoding='utf-8') as f:
f.write(chapter_name)
f.write('\n')
f.write('\n'.join(content))
f.write('\n')
from bs4.builder import HTMLTreeBuilder
import requests
import time
from tqdm import tqdm
from bs4 import BeautifulSoup
def get_content(target):
req = requests.get(url = target)
req.encoding = 'utf-8'
html = req.text
bf = BeautifulSoup(html, 'lxml')
texts = bf.find('div', id='content1')
content = texts.text.strip().split('\xa0'*4)
return content
if __name__ == '__main__':
server = 'https://www.xuanshu.com/book/78071/'
book_name = 'D://txt/掌门师叔不可能是凡人.txt'
target = 'https://www.xuanshu.com/book/78071/'
#html = open('D://txt/url.html','r',encoding='utf-8')
req = requests.get(url = target)
req.encoding = 'utf-8'
html = req.text
chapter_bs = BeautifulSoup(html, 'lxml')
chapters = chapter_bs.find('div', class_='pc_list')
chapters = chapters.find_all('a')
for chapter in tqdm(chapters):
chapter_name = chapter.string
url = server + chapter.get('href')
content = get_content(url)
with open(book_name, 'a', encoding='utf-8') as f:
f.write(chapter_name)
f.write('\n')
f.write('\n'.join(content))
f.write('\n')