Python实现爬取逐浪小说的方法
本文实例讲述了Python实现爬取逐浪小说的方法。分享给大家供大家参考。具体分析如下:
本人喜欢在网上看小说,一直使用的是小说下载阅读器,可以自动从网上下载想看的小说到本地,比较方便。最近在学习Python的爬虫,受此启发,突然就想到写一个爬取小说内容的脚本玩玩。于是,通过在逐浪上面分析源代码,找出结构特点之后,写了一个可以爬取逐浪上小说内容的脚本。
具体实现功能如下:输入小说目录页的url之后,脚本会自动分析目录页,提取小说的章节名和章节链接地址。然后再从章节链接地址逐个提取章节内容。现阶段只是将小说从第一章开始,每次提取一章内容,回车之后提取下一章内容。其他网站的结果可能有不同,需要做一定修改。在逐浪测试过正常。
现分享此代码,一是做个记录,方便自己以后回顾。二么也想抛砖引玉,希望各路大神不吝赐教。
#-*-coding:utf8-*- #!/usr/bin/python # Python: 2.7.8 # Platform: Windows # Program: Get Novels From Internet # Author: wucl # Description: Get Novels # Version: 1.0 # History: 2015.5.27 完成目录和url提取 # 2015.5.28 完成目录中正则提取第*章,提取出章节链接并下载。在逐浪测试下载无误。 from bs4 import BeautifulSoup import urllib2,re def get_menu(url): """Get chapter name and its url""" user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0" headers = {'User-Agent':user_agent} req = urllib2.Request(url,headers = headers) page = urllib2.urlopen(req).read() soup = BeautifulSoup(page) novel = soup.find_all('title')[0].text.split('_')[0] # 提取小说名 menu = [] all_text = soup.find_all('a',target="_blank") # 提取记载有小说章节名和链接地址的模块 regex=re.compile(ur'\u7b2c.+\u7ae0') # 中文正则匹配第..章,去除不必要的链接 for title in all_text: if re.findall(regex,title.text): name = title.text x = [name,title['href']] menu.append(x) # 把记载有小说章节名和链接地址的列表插入列表中 return menu,novel def get_chapter(name,url): """Get every chapter in menu""" html=urllib2.urlopen(url).read() soup=BeautifulSoup(html) content=soup.find_all('p') # 提取小说正文 return content[0].text if __name__=="__main__": url=raw_input("""Input the main page's url of the novel in ZhuLang\n Then Press Enter to Continue\n""") if url: menu,title=get_menu(url) print title,str(len(menu))+'\n Press Enter To Continue \n' # 输出获取到的小说名和章节数 for i in menu: chapter=get_chapter(i[0],i[1]) raw_input() print '\n'+i[0]+'\n' print chapter print '\n'
希望本文所述对大家的Python程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.
