• python小说爬虫源代码


    利用python来获取网络小说是非常方便的,编程也不难,稍微改一下代码内容,基本上网络小说都能抓取下来。留下源代码方便以后使用。

    1. # -*- coding: utf-8 -*-
    2. """
    3. 《武灵天下》网络小说爬虫.
    4. """
    5. from bs4 import BeautifulSoup
    6. import requests
    7. import time
    8. import os
    9. from threading import Thread
    10. headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36'}
    11. saveDir = 'd:\\武灵天下\\'
    12. url = 'http://www.yuyouge.com/book/39764/' #《武灵天下》 目录页
    13. # 获得所有主题的URL
    14. def get_topic(url):
    15. id = 0
    16. threads = []
    17. response = requests.get(url, headers=headers)
    18. response.encoding = response.apparent_encoding
    19. soup = BeautifulSoup(response.content, 'html.parser')
    20. tag_lis = soup.find('ul', id='chapters-list').find_all('li')
    21. for tag_li in tag_lis:
    22. tag_a = tag_li.find('a')
    23. if tag_a:
    24. topic_url = tag_a.get('href') # 获得主题链接
    25. topic_url = 'https://www.yuyouge.com'+topic_url # 主题页面 url
    26. txt = str.replace(tag_li.find('a').get_text(), 'VIP_', '') # 主题名称
    27. txt = str.replace(txt, '正文_', '') # 主题名称
    28. if txt[0] == '第':
    29. print(topic_url +' ' + txt)
    30. id += 1
    31. # get_txt(id, topic_url, txt)
    32. thread = Thread(target=get_txt, args=(id, topic_url, txt)) # 线程调用文本下载函数,下载主题页面中的文本内容
    33. thread.start()
    34. threads.append(thread)
    35. print(id)
    36. for t in threads:
    37. t.join()
    38. # 下载链接页面中的文本内容
    39. def get_txt(id, url, txt):
    40. #print(response.encoding)
    41. txt = str.replace(txt, '/', ' ')
    42. txt = str.replace(txt, '\\', ' ')
    43. txt_file = '%s%04d-%s.txt'%(saveDir, id, txt)
    44. # 如果目录不存在,就新建目录
    45. if not os.path.exists(saveDir):
    46. os.makedirs(saveDir)
    47. # 文件不存在就下载,否则跳过
    48. if not os.path.exists(txt_file):
    49. response = requests.get(url, headers=headers)
    50. response.encoding = response.apparent_encoding
    51. soup = BeautifulSoup(response.content, 'html.parser')
    52. tag_txt = soup.find('div', id='txtContent').get_text(separator='\n')
    53. # i = tag_txt.find('www.qianyege.com')
    54. # tag_txt = txt + tag_txt[i+16:]
    55. # print(tag_txt)
    56. with open(txt_file, mode='w', encoding='utf8') as f:
    57. size = f.write(tag_txt)
    58. f.close()
    59. print("Threads-(%d) : %s(%d Bytes)"%(id, txt_file, size))
    60. if __name__ == '__main__':
    61. print('===== 主程序开始 =====')
    62. start = time.perf_counter()
    63. get_topic(url)
    64. # get_txt(12,'https://www.yuyouge.com/book/39764/135943672.html', '第2020章 如影随形')
    65. tt = time.perf_counter() - start
    66. print('===== 主程序结束,用时 {:.2f}s ====='.format(tt))

  • 相关阅读:
    代码随想录1刷—字符串篇
    Java基础浅聊Future带来的异步优点和缺点
    反序列化漏洞(3), CTF夺旗
    LeetCode 面试题 03.03. 堆盘子
    Config
    这6点建议,助你解决海外客户的视频验厂!
    六、数据仓库详细介绍(ETL)方法篇
    Camera2相关知识总结
    nodejs+vue+elementui电影在线播放交流网站express
    螺杆支撑座应用领域合集
  • 原文地址:https://blog.csdn.net/yangliu/article/details/134031761