https://fanqienovel.com/page/7111874278269652002?enter_from=stack-room,请用python编写代码爬取上述网址的前10章内容。
时间: 2023-12-18 22:03:00 浏览: 36
好的,我可以帮你编写爬虫代码。不过在开始之前,需要先安装 requests 和 BeautifulSoup 这两个库。可以通过以下命令进行安装:
```
pip install requests
pip install beautifulsoup4
```
安装完成后,可以使用以下代码来爬取前10章内容:
```python
import requests
from bs4 import BeautifulSoup
url = "https://fanqienovel.com/page/7111874278269652002?enter_from=stack-room"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取前10章的链接
chapter_links = []
for link in soup.find_all('a', class_='chapter-list-item'):
chapter_links.append(link['href'])
if len(chapter_links) == 10:
break
# 爬取每一章的内容
for link in chapter_links:
response = requests.get(link)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取章节标题
chapter_title = soup.find('h2', class_='chapter-title').text.strip()
print(chapter_title)
# 获取章节内容
chapter_content = soup.find('div', class_='chapter-content').text.strip()
print(chapter_content)
print('-----------------------')
```
这段代码会输出前10章的标题和内容。注意:爬取别人网站的内容需要得到网站的许可,未经允许的情况下不要随意爬取网站内容。