# coding:utf-8 import re import time import requests import lxml.html from lxml import etree urls = ['https://www.xxddxs.com/book/{}'.format(i) for i in range(1, 20)] path = r'D:\py\venv\Scripts\xiaoshuo' # path = './' sleep = 1 def get_text(url): r = requests.get(url) r.encoding = 'utf-8' selector = etree.HTML(r.text) # Get the book title from the page book_info = selector.xpath('/html/body/div[5]')[0] title = book_info.xpath("//h1")[0].text # title = .xpath('/html/body/div[5]/div[2]/h1') print(title) # If no title found, print an error message and return early if len(title) == 0: print(f"No title found for {url}") return # Get the text of the chapter chapter_list = selector.xpath('/html/body/div[5]/dl/dd/a') last_chapter_num = re.search(r'(\d*)\.html$',chapter_list[-1].attrib.get('href')).group(1) # Write the text to a file in the specified directory with open(title + ".txt", 'w', encoding='utf-8') as f: for num in range(1, int(last_chapter_num)+1): content_url = url + f'/{num}.html' print(content_url) time.sleep(sleep) # 防止请求量过多导致网页拒绝访问 try: r = requests.get(content_url, timeout=30) except: print('连接失败,重试一次') r = requests.get(content_url, timeout=30) r.encoding = 'utf-8' content_dom = lxml.html.fromstring(r.text) content = content_dom.xpath('//*[@id="chaptercontent"]')[0].text_content() f.writelines(content) if __name__ == '__main__': for url in urls: get_text(url) 这个代码的运行思路
时间: 2024-04-02 13:36:16 浏览: 17
这个代码的运行思路如下:
1. 定义需要爬取小说的URL列表和保存小说的文件路径
2. 定义一个函数 `get_text` 用来获取小说内容,其中包括小说标题,小说章节信息和小说正文
3. 在 `get_text` 函数中,先通过URL获取小说页面的HTML文本,然后使用XPath解析HTML文本获取小说的标题和章节信息
4. 然后通过正则表达式获取小说最新章节的编号,并根据章节编号遍历获取每一章节的正文
5. 最后将小说的标题和正文保存到指定的文件路径中
6. 在主程序中,循环遍历URL列表,调用 `get_text` 函数来获取每个小说的内容并保存到文件中。
相关问题
# coding=utf-8 和 # coding: utf-8有什么区别
两者的作用是相同的,都是用来指定Python源文件的编码格式为UTF-8。但是,它们的语法有所不同:
- `# coding=utf-8` 使用了等号,这种写法在Python 2.x中是不支持的,只有在Python 3.x中才能使用。
- `# coding: utf-8` 使用了冒号,这种写法在Python 2.x和Python 3.x中都是支持的。
在Python 2.x中,如果你想指定文件编码格式为UTF-8,应该使用第二种写法,即 `# coding: utf-8`。在Python 3.x中,两种写法都可以使用,但是推荐使用第一种写法,即 `# coding=utf-8`。
#coding=utf-8 from requests_html import HTMLSession session = HTMLSession() import time import csv import os 什么意思
这段代码的作用是导入所需的模块和库,以及设置Python源文件的编码格式为UTF-8。
- `from requests_html import HTMLSession` 导入了名为`HTMLSession`的类,该类可以用于进行HTTP请求和解析HTML内容。
- `session = HTMLSession()` 创建了一个`HTMLSession`的实例对象,用于发送HTTP请求和处理响应。
- `import time` 导入了Python内置的`time`模块,用于进行时间相关的操作,如暂停程序执行。
- `import csv` 导入了Python内置的`csv`模块,用于读写CSV文件。
- `import os` 导入了Python内置的`os`模块,用于与操作系统进行交互,如文件操作和路径操作。
最后一行 `#coding=utf-8` 是一个注释,它表示该源文件的编码格式是UTF-8。这是一种常见的编码格式,用于支持多种语言和字符集。在Python 3中,通常不需要显式地声明编码格式,因为默认编码格式就是UTF-8。但是在旧版本的Python或特定环境中,可能需要添加这样的注释来确保正确的编码处理。