红楼梦 第11-15回 网址爬取及文本保存
红楼梦 第11-15回 网址爬取及文本保存
以下列出红楼梦第11回至第15回的网站链接,以及爬取这些章节的文本内容并保存到 hlm.txt 文件的示例代码:
链接:
- 第十一回: https://www.shicimingju.com/book/hongloumeng.html
- 第十二回: https://www.shicimingju.com/book/hongloumeng.html
- 第十三回: https://www.shicimingju.com/book/hongloumeng.html
- 第十四回: https://www.shicimingju.com/book/hongloumeng.html
- 第十五回: https://www.shicimingju.com/book/hongloumeng.html
示例代码:
import requests
from bs4 import BeautifulSoup
# 目标网站链接
urls = [
'https://www.shicimingju.com/book/hongloumeng.html',
'https://www.shicimingju.com/book/hongloumeng.html',
'https://www.shicimingju.com/book/hongloumeng.html',
'https://www.shicimingju.com/book/hongloumeng.html',
'https://www.shicimingju.com/book/hongloumeng.html'
]
# 获取文本内容并写入文件
with open('hlm.txt', 'w', encoding='utf-8') as f:
for i, url in enumerate(urls, start=11):
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
# 这里需要根据目标网站的结构修改代码来获取文本内容
text = soup.find('div', class_='chapter-content').get_text().strip() # 示例代码,需要根据实际情况修改
f.write(f'第{i}回 {text}
')
print(f'第{i}回 爬取成功!!!')
说明:
- 以上代码仅供参考,需要根据目标网站的结构进行调整。
- 请根据实际情况修改代码中的
class_属性值,以便获取正确的文本内容。 - 可以根据需要修改
hlm.txt文件的名称。
注意:
- 在爬取网站内容之前,请务必遵守网站的爬取规则,避免对网站造成负面影响。
- 爬取网站内容时,请尊重版权,不要进行商业用途。
原文地址: https://www.cveoy.top/t/topic/o5mZ 著作权归作者所有。请勿转载和采集!