import requests import parsel # 数据解析模块 from tqdm import tqdm # 进度条显示模块 def get_response(html_url): """发送请求函数""" response = requests.get(html_url) return response def get_novel_url(html_url): """获取所有章节url地址""" response = get_response(html_url) selector = parsel.Selector(response.text) href = selector.css('#list dd a::attr(href)').getall() return href def get_novel_content(html_url): """获取小说的内容""" response = get_response(html_url) selector = parsel.Selector(response.text) title = selector.css('.bookname h1::text').get() content_list = selector.css('#content::text').getall() content = '\n'.join(content_list) novel_content = [title, content] return novel_content print(title) def save(name, title, content): """保存数据""" with open(name + '.txt', mode='a', encoding='utf-8') as f: f.write(title) f.write('\n') f.write(content) f.write('\n') def main(html_url): """主函数""" response_1 = get_response(html_url) selector = parsel.Selector(response_1.text) name = selector.css('#info h1::text').get() href = get_novel_url(html_url) # 调用了获取小说章节url地址的函数 for index in tqdm(href): index_url = 'https://www.biqugee6.com' + index novel_content = get_novel_content(html_url) # 调用了获取小说内容的函数 save(name, novel_content[0], novel_content[1]) # 调用了保存函数 url = 'https://www.biqugee6.com/book/657/' main(url) 报错显示:
Traceback (most recent call last):
File "H:\Pycharm\Project\Python\Novel\Novel.py", line 73, in <module>
main(url)
File "H:\Pycharm\Project\Python\Novel\Novel.py", line 69, in main
save(name, novel_content[0], novel_content[1]) # 调用了保存函数
File "H:\Pycharm\Project\Python\Novel\Novel.py", line 54, in save
f.write(title)
TypeError: write() argument must be str, not None
求解决!!